View Single Post
  #14  
Old 08-29-2008, 01:09 AM
Programmer Programmer is offline
Coder
Programmer's Avatar
Join Date: Jan 2008
Location: -78.464422, 106.837328
Posts: 449
Programmer has a spectacular aura aboutProgrammer has a spectacular aura about
Send a message via AIM to Programmer Send a message via MSN to Programmer Send a message via Yahoo to Programmer
Quote:
Originally Posted by GULTHEX View Post
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.

PHP Code:
180

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:
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) * 
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:
+= changeX
+= 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 mod** 90)

// This is for an 8-way movement
-= (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 View Post
Post
I think this is a little too complicated for him to understand.
__________________
- Iᴀɴ Zɪᴍᴍᴇʀᴍᴀɴ

Last edited by Programmer; 08-29-2008 at 01:25 AM.. Reason: Grammar, Ian! Grammar!
Reply With Quote