I am making an Asteroids-like game in OpenGL, using LWJGL. I have a box and control handling. Next step: Motion. I have it set so that left arrow rotates left and right arrow rotates right. Now I need Up arrow to go.
I have it set to go straight if it is headed straight up, down, left, or right:
Code:
public void go(){
float r = -(this.r) % 360;
r = r < 0 ? 360 + r : r;
float dx, dy;
System.out.println(r);
if(r == 0){ y += 10; return;}
if(r == 90){ x += 10; return;}
if(r == 180){ y -= 10; return;}
if(r == 270){ x -= 10; return;}
}
but now what if it is at an angle? I need to do several things:
- Determine slope
- Convert it to
delta Y / delta X
- Find the equivalent fraction whose numerator and denominator are
a and b in a pythagorean triple where c = 10 (or close)
Then I can adjust its position by those amounts
How should I approach this problem?
I am aware that slope is equal to
tan(Θ), but I have no clue where to begin making it into a fraction.
I could loop through all possible fractions until I get one that is equivalent and a pythagorean triple, but (correct me if I'm wrong) that wouldn't be fast enough for use in a game (currently 20 ticks per second)