I'm a new game developer and I have always wondered about how would I go about making a 'Rectangle' object move at a certain angle? I thought I could make the x increase rapidly and the y increase at a delay, but how would I do that?
Asked
Active
Viewed 702 times
-3
-
2Did you try anything? What was the result? – Oliver Charlesworth Jun 12 '14 at 21:19
-
2In its current form, this question is too open ended to be answered. – Andreas Jun 12 '14 at 21:19
1 Answers
1
Trigonometry
is your friend:
float angle = 64.0;
float distance = 10.0;
float x_movement = 0.0;
float y_movement = 0.0;
x_movement = distance * cos(angle);
y_movement = distance * sin(angle);
/*
* But if the function accepts only radians then…
* PI = 3.14159265359
*/
x_movement = distance * cos((angle/180) * PI);
y_movement = distance * sin((angle/180) * PI);
your_rectangle.moveX(x_movement);
your_rectangle.moveY(x_movement);
This a pseudo-code, of course. Also, this is more math-related rather than a programming question per se.
![](../../users/profiles/2625090.webp)
arielnmz
- 6,592
- 7
- 29
- 54