-3

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?

1 Answers1

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.

arielnmz
  • 6,592
  • 7
  • 29
  • 54