2005-04-14 12:43:01

Hello

All,
I am trying to work out a method that will move an object (IE: update

its coordinates) based on the course of the object. For instance, if an

object is facing forty-five degrees (Northeast,) and it moves, the X

coordinate of that object will be incremented by 0.5, and same for the Y

coordinate. Thus, it would move relative to (0, 0), (0.5, 0.5), (1, 1),

(1.5, 1.5). I need to work out an algorithm to accomplish this. The current

one I have come up with is a no go. Any help would be greatly appreciated!

2005-04-14 17:29:55 (edited by Davy 2005-04-14 21:58:10)

You

need to parametrize the direction the object is moving into radial

co

2005-04-14 22:38:43

Hello,
Can I just exclude the speed and timeElapsed variables or will the

formula not work that way? For instance, let us say I declared a method like

this:
public void MoveObject(double X,double Y,int Direction){
X = X *

Cos(Direction)
Y = Y * Sin(Direction)
}

This method would expect X, Y,

and the course of the object (Direction) as parameters and update the

coordinates. Would this work? Let us assume that the object was headed

North-Northeast (about 35.) This means that Direction would be equal to 35.

If I understand you correctly, this should be the argument passed to

Cos/Sin.
Thanks.

2005-04-15 18:31:30

Let us

say we implement the method as follows :

public void MoveObject(int

directionInDegrees, double speed, double timeElapsed)
{
double

directionInRad = (double) directionInDegrees*2.0*Pi/360.0;
object.X =

object.X + speed * Cos(directionInRad) * timeElapsed;
object.Y = object.Y +

speed * Sin(directionInRad) * timeElapsed;
}

The need for the speed and

timeElapsed variables depends on when you want to call this method. The

speed variable indicates how far you want to move the object. If the speed

is constant, you can replace it with a fixed number.
If you call this

method in an infinate game loop (multiple times per second) and you need to

make sure that the object doesn't move faster on faster computers, you need

the timeElapsed variable. If you call the MoveObject method as a response of

an action, e.g. to take a single step, you can ommit the timeElapsed

variable.

Hope this helps,
Davy

2005-04-16 01:02:48

Hello,
Thanks much; I will try it out and let you know how it goes.

2005-04-17 12:40:29

hi,

excuse me, but i want to ask i learned vb, but i didn't find it as useful

as c. so, how can i learn c?
thanks a lot, parham doostdar!

2005-04-17 21:17:28

www.blindprogramming.com has some excellent resources. If you're a member

of bookshare.org, try checking their stocks as well.