Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Oct 2011
    Posts
    106
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Rotation to slope

    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:
    1. Determine slope
    2. Convert it to delta Y / delta X
    3. 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)

  • #2
    Regular Coder
    Join Date
    Oct 2011
    Posts
    106
    Thanks
    12
    Thanked 0 Times in 0 Posts
    I continued to do research, and as my search got less and less specific, I found an example for Flash AS3 (see link). Unless this is helpful to people who come across it later, please delete this thread.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •