## ending location from angle and speed

Nibbie
Posts: 1
Joined: 2009.03
Post: #1
I'm trying to calculate the ending x,y of my object. I have the starting x,y and the angle in radians and the speed

this is what I'm trying:

Code:
```deltaX = cos( rotation ) * speed; deltaX = startingX * deltaX; deltaY = sin( rotation ) * speed; deltaX = startingY * deltaY;```

the result is almost as if everything is wildly exagerated.

does anyone know how to calculate this correctly?
Moderator
Posts: 1,563
Joined: 2003.10
Post: #2
Seems like you'd want to add deltaX/Y to startingX/Y instead of multiplying them together.
Member
Posts: 260
Joined: 2005.05
Post: #3
Also, why multiply the *speed* by cos and sin of the angle? What are you trying to do there? Should the translation be affected by rotation?

I usually express this kind of things by matrix multiplication. It scales better to more complex situations.
Member
Posts: 306
Joined: 2009.03
Post: #4
I have a facing vector instead of a facing angle. The plus side is you don't need to cos and sin constantly, only when they turn. Then when you want to move somebody you just do
movement=[facing scale:speed];

I just made up arbitrary function names but you can probably get the point.
When you want to turn all you need to do is rotate the facing vector by X radians negative for a left turn, positive for a right turn. You can rotate a vector using CGAffineTransform functions.
Nibbie
Posts: 1
Joined: 2009.03
Post: #5
Kazooless Wrote:I'm trying to calculate the ending x,y of my object. I have the starting x,y and the angle in radians and the speed

this is what I'm trying:

Code:
```deltaX = cos( rotation ) * speed; deltaX = startingX * deltaX; deltaY = sin( rotation ) * speed; deltaX = startingY * deltaY;```

the result is almost as if everything is wildly exagerated.

does anyone know how to calculate this correctly?

I assume this is so that you can move an object on an angle? if so you can use the following code
Code:
```deltaY = sin (rotation) * speed; //this finds the y travel distance finalY = startingY + deltaY; // this is the location of object on the y plane deltaX = cos (rotation)  * speed; //this finds the x travel distance finalX = startingY + deltaY;```

It's basic trigonometry where the speed is the hypoteneuse and the y is "Opposite" and x is "Adjacent".
looking at a clock 3 o'clock would be 0 (or 360) degree, 12 - 90 (or -270) degrees, 9 - 180 (or -180) degrees, 6 - 270 (or -90) degrees.
speed * cos (rotation)
Member
Posts: 73
Joined: 2009.03
Post: #6
masdest Wrote:I assume this is so that you can move an object on an angle? if so you can use the following code
Code:
```deltaY = sin (rotation) * speed; //this finds the y travel distance finalY = startingY + deltaY; // this is the location of object on the y plane deltaX = cos (rotation)  * speed; //this finds the x travel distance finalX = startingY + deltaY;```

It's basic trigonometry where the speed is the hypoteneuse and the y is "Opposite" and x is "Adjacent".
looking at a clock 3 o'clock would be 0 (or 360) degree, 12 - 90 (or -270) degrees, 9 - 180 (or -180) degrees, 6 - 270 (or -90) degrees.
speed * cos (rotation)

`finalX = startingX + deltaX;`