Quote:
Originally Posted by GULTHEX
say what
|
This type of mathematics is quite difficult to understand at first, but it will get easier. For the purposes of this script, I will be using Trigonometry to get the appropriate answer.
Say we have angle
a and speed
s, and we wanted to get the changing X and the changing Y values for said speeds.
For these purposes, I will assign the following values to
a and
s.
From this, we can work out what the changing X and changing Y are.
First off, we want to convert
a into radians so that they will work with Sine and Cosine. This is done using the following formula*:
PHP Code:
r = a * pi / 180
Now that we have the radian value of the angle (now referred to as
r), we can continue on.
To get a simple speed (if
s was 1, for example), you would use the following formula:
PHP Code:
changeX = sin(r)
changeY = -cos(r)
Now, if you wanted to alter the speed to the value specified in
s, simply multiply it in the formula. You end up with the following:
PHP Code:
changeX = sin(r) * s
changeY = -cos(r) * s
From this, you will be able to get your new position using the speed specified. To do this, simply add it to your object's X and Y values, like so:
PHP Code:
x += changeX
y += changeY
Now, you may only want your object to move in 4 ways or in 8 ways, or even up to an infinite amount of ways. If you wish to put directional restrictions on your object, do the following before converting to radians:
PHP Code:
// This is for a 4-way movement.
a -= (a mod** 90)
// This is for an 8-way movement
a -= (a mod** 45)
Hope this was helpful
(NOTE: With the values specified, if you followed instructions carefully enough, you should get changeX = 0 and changeY = 5.)
* In GraalScript, this functionality is built-in. You can either use the formula or use degtorad(float) to get your answer.
** Replace this with a percent sign in GraalScript.
Quote:
Originally Posted by DrakilorP2P
Post
|
I think this is a little too complicated for him to understand.