Let me explain it to you:
This website here demonstrates finding the math.cos
and math.sin
of an angle.
as you can see, the math.sin()
represents the y increase if you moved in a 2-dimensional direction of theta (the angle) and the same for math.cos
. Now, notice of the radius of that circle is ALWAYS 1. Since we know that 1 times any number is always going to be that number, if we just multiply the vx
and vy
values by speed
you’ll increase the size of that circle, and move farther.
These are just simple trigonomic functions. Understanding math.sin()
and math.cos()
is crucial to any spatial related programming.
Now, all we have to do to implement gravity, is over time, change that angle of movement. There are two ways you can do this, first, the method I used by just subtracting of off the vx
and vy
, or two, subtracting of the ORIGINAL angle, and from there finding the math.cos()
and math.sin()
of that angle.
Example:
if (math.pi/2) < angle and angle < (3*math.pi/2) then
angle += math.pi /180 -- one degree
else if (math.pi/2) > angle or (3*math.pi.2) < angle then
angle -= math.pi/180 -- one degree
end
What this script does, is checks if the current moving angle is greater than 90 degrees and less than 270 degrees (moving down), then it adds a degree to the movement of the angle. Else, if it’s less than 90 degrees, OR greater than 270 degrees, it subtracts a degree from the angle.