Game maker lengthdir inner implementation - game-maker

I wonder how lenghtdir_x/y is implemented. To my understanding this has something to do with trigonometry but since I'm a fairly new to all of this I can't figure it out myself.

You are correct, it is a bit of trigonometry - namely, converting from degrees to radians, and then multiplying a sine/cosine of that angle by "length" (hence why it is called length-dir)
lengthdir_x(l, d) is l * cos(d * pi / -180)
lengthdir_y(l, d) is l * sin(d * pi / -180)
live demo for comparison

Related

Calculate distance between two latitude-longitude points with SQLite (almost Haversine formula)

Recently I started a project where I need to calculate the distance between two points given their latitude and longitude and select and order the points by distance.
The logical step to do this is to use the Haversine formula:
Distance = 2 * R * ASIN( SQRT( SIN( (RADIANS(lat1)-RADIANS(lat2))/2 )^2 + COS( RADIANS(lat1) )*COS( RADIANS(lat2) )*SIN( (RADIANS(long1)-RADIANS(long2))/2 )^2 ) )
This is not a problem for MySQL DB, but for some reasons I needed to use SQLITE database and I was building my project with Laravel.
The problem with SQLITE is that has few math functions, and none trigonometric functions (sin, cos, etc.)
At first I thought of using SQLITE extensions, but it seems that PDO that is used by Laravel for DB connections does not allow to load them.
Then I tried PHP createFunction to define functions in SQLite, but, as far as I understand it only works with scalars, that means it does not accept fields names as argument of the defined functions.
I was stucked.
I needed to work with small distances (less than 20km) and I did not need high precision. So a plane geometry was enough for me, but how?
At last, I remembered that sin(angle) ~ angle if angle is small. So I went through the Haversine formula trying to simplify it. It seems a spherical solution of the Pythagorean theorem.
It is done by two parts, the first one defines the difference in latitude, let's say the Y.
Y = R * ABS(RADIANS(lat1)-RADIANS(lat2))
The second part defines the difference in longitude, let's say the X.
X = R * COS(RADIANS(lat0)) * ABS(RADIANS(long1)-RADIANS(long2))
In this part there is a COS since the radius of the circle changes with the latitude. It could be a problem since I said that SQLite does not have that function, but since we are using small variations of the angles lat0 can be a constant value, and calculated once for ever.
Since I have one point given by the user I can put lat0 = lat1 and calculate COS(lat1) before the query or you can define it for your location.
The last thing is to calculate the Estimated_Distance^2 = X^2 + Y^2. Well, SQLite does not even have the SQRT functions, so you must keep it elevated by 2...
The final formula is:
Estimated_Distance = SQRT ( ( R * ABS(RADIANS(lat1)-RADIANS(lat2)) ) ^ 2 + ( R * COS(RADIANS(lat0)) * ABS(RADIANS(long1)-RADIANS(long2)) ) ^ 2 )
The result is not too bad, the error against the Haversine formula is less than 1 m when the distance is less than 10-15 km, but it grows fast with higher distances.
When I will move the project on a production server I'll use MySQL and the correct formula, in the meanwhile this is good enough for me.
If you have any suggestion to improve it, please let me know.
I hope this can help.

F# precise calculating with decimals?

I'm trying to do a precise calculation with floats like
let pi = double (22/7)
printfn "%f" (cos(2.00*pi*1.00/2.00))
// output: -0.989992
On a calculator I get -1, so it can round up and down correctly, however, when I do this in F# I get the result/output: -0.989992 which is close to -1, but how do I get an output -1 so it rounds up and down correctly?
I tried to read about the topic and it seems like I need to import a module, can this be true?
Your calculation is off not because of rounding error, but because 22/7 is a very loose approximation of the value of π.
22/7 = 3.142857142857...
π = 3.14159265358979...
22/7 - π = 0.00126448927...
Wolfram Alpha uses a much better approximation of π than 22/7, so that's why your calculation is showing different results from Wolfram Alpha.
Instead of doing let pi = double (22/7), you should just use System.Math.PI (e.g., let pi = System.Math.PI). That will get you an accurate value for (cos(2.00*pi*1.00/2.00)). No need for rounding.
See the docs for Math.PI for more details.
So the question has a few problems.
As others point out 22/7 is just an approximation of PI
Aslo, let pi = double(22/7) results in pi = 3.0. This because 22/7 is integer division in F#.
When comparing with wolfram the expression uses a better approximation of PI than 3.0 meaning the F# result differs from wolfram rather significantly
When asking Wolfram and F# to compute the same expression: cos(3) the result is as following.
F#: cos 3.0 => -0.989992496600445
Wolfram: cos(3) => -0.98999249660044545727157279473126130239367909661558832881
Wolfram do compute more decimals but we see that the numbers only differs by > 1e-15
When we ask F# and Wolfram to what cos(pi) is they are in agreement:
F#: cos System.Math.PI => -1.0
Wolfram: cos(pi) => -1
I found the way, sorry! You need to use System.Math.Round. No need to import anything as its already built-in.
I.e.: System.Math.Round(System.Math.Round(cos((2.00*pi*2.00/ 2.00)), 0))

trying to control kiwi robot by ROS

I would like to help me in this problem.
I am working on three wheels omni robot project , I am using arduino as a brain with ROS (Robot Operating System), but I am facing a problem in the kinematics of the robot,when I am trying to control the robot via twist teleop it works well for all the keys except I and < (these keys must stop the second motor but the motor stills moving).
Below is the part code that returns to the kinematics I used.
Any help would be greatly appreciated.
void omni_drive(double x, double y, double theta) {
float vc = -0.5 * vx - sqrt(3)/2 * vy+theta;
float vb = -0.5 * vx + sqrt(3)/2 * vy+theta;
float va = vx+theta;
}

What order of precedence does ∑ have?

Trying to implement a neural network algorithm here, but I'm a bit lost on the math side of things:
Note that p and i should be subscript (not sure how to do it in stackoverflow).
(ΣpΣi(tpi - opi)^2) / (n * k)
Basically my question is concerning the inner ∑ : Do I sum (for all i (tpi - opi)^2)? Or do I square (the sum for all i (tpi - opi))?
Sum of squares. So Σi(tpi - opi)^2 means (tp1 - op1)^2 + ... + (tpn - opn)^2. If you wanted square of sum it would be written most likely as (Σi(tpi - opi))^2. Also since its neural nets you probably mean the sum of squares.

Calculate the position of an accelerating body after a certain time [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
How do I calculate the position of an accelerating body (e.g. a car) after a certain time (e.g. 1 second)?
For a moving body that it not accelerating, it is a linear relationship, so I presume for an accelerating body it involves a square somewhere.
Any ideas?
The equation is: s = ut + (1/2)a t^2
where s is position, u is velocity at t=0, t is time and a is a constant acceleration.
For example, if a car starts off stationary, and accelerates for two seconds with an acceleration of 3m/s^2, it moves (1/2) * 3 * 2^2 = 6m
This equation comes from integrating analytically the equations stating that velocity is the rate-of-change of position, and acceleration is the rate-of-change of velocity.
Usually in a game-programming situation, one would use a slightly different formulation: at every frame, the variables for velocity and position are integrated not analytically, but numerically:
s = s + u * dt;
u = u + a * dt;
where dt is the length of a frame (measured using a timer: 1/60th second or so). This method has the advantage that the acceleration can vary in time.
Edit A couple of people have noted that the Euler method of numerical integration (as shown here), though the simplest to demonstrate with, has fairly poor accuracy. See Velocity Verlet (often used in games), and 4th order Runge Kutta (a 'standard' method for scientific applications) for improved algorithms.
Well, it depends on whether or not acceleration is constant. If it is it is simply
s = ut+1/2 at^2
If a is not constant, you need to numerically integrated. Now there is a variety of methods and none of them will beat doing this by hand for accuracy, as they are all ultimately approximate solutions.
The easiest and least accurate is Euler's method . Here you divide time into discrete chunks called time steps, and perform
v[n] = v[n-1] * t * a[t]
n is index, t is size of a time step. Position is similarly updated. This is only really good for those cases where accuracy is not all that important. A special version of Euler's method will yield an exact solution for projectile motion (see wiki), so while this method is crude, it can be perfect for some suituations.
The most common numerical integration method used in games and in some chemistry simulations is Velocity Verlet, which is a special form of the more generic Verlet method. I would recommend this one if Euler's is too crude.
In this article: http://www.ugrad.math.ubc.ca/coursedoc/math101/notes/applications/velocity.html (webarchive), you can find this formula:
p(t) = x(0) + v(0)*t + (1/2)at^2
where
p(t) = position at time t
x(0) = the position at time zero
v(0) = velocity at time zero (if you don't have a velocity, you can ignore this term)
a = the acceleration
t = your current itme
Assuming you're dealing with constant acceleration, the formula is:
distance = (initial_velocity * time) + (acceleration * time * time) / 2
where
distance is the distance traveled
initial_velocity is the initial velocity (zero if the body is intially at rest, so you can drop this term in that case)
time is the time
acceleration is the (constant) acceleration
Make sure to use the proper units when calculating, i.e. meters, seconds and so on.
A very good book on the topic is Physics for Game Developers.
Assuming constant acceleration and initial velocity v0,
x(t) = (1/2 * a * t^2) + (v0 * t)

Resources