• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Angle comparison problem

G

graviax

Guest
My project is a topview action game.
The player can move freely in any direction.
I wanted to add a "speed" mode, where the player, instead of moving pending on your keyboard direction (up down left right), could move pending on the direction of the mouse.
He would be a lot faster than walk mode the counter part being that if you change direction quickly you will lose speed. the amount of speed you lose is determined by ratio :
Code:
acceleration = 1 / ((speed * 10) + 1);
ratio = 1 - (deltaAngle * (speed / 2 + 1) / 180);
speed += acceleration * ratio;
In *theory* this should work, but angles are between 0 and 360 for the game.
It understand when you give it something above 360 or below 0 but he would change that value to something between 0 and 360.
Here's my problem, I don't know how to work with that.
If I do a comparison like that :
Code:
if(mouse_angle < desired_angle)
{
     do that
}
else
{
     do this
}
It would not work.
I came up with an other way to do it, but in the end I have the same issue.


here's my code (not working as intended):
Code:
aimtab[0] = aimtab[1];
    aimtab[1] = point_direction(x,y,mouse_x,mouse_y);
    acc = 1/((vitesse*10)+1);
    angle_direction = aimtab[0] + 180;
    if(angle_direction >= 360)
    {
        angle_direction -= 360;
    }
    if(aimtab[1] > aimtab[0] && aimtab[1] < angle_direction)
    {
        ratio = (1 - ((aimtab[1] - aimtab[0]) * (vitesse/2+1)/180));
        draw_set_color(c_black);
    }
    else
    {
        ratio = -(1 - ((aimtab[0] + (360 - aimtab[1])) * (vitesse/2+1)/180));
        draw_set_color(c_white);
    }
    vitesse += acc * ratio;
    if(vitesse > 10)
    {
        vitesse = 10;
    }
    if(vitesse < 0)
    {
        vitesse = 0;
    }
    x += lengthdir_x(vitesse,aimtab[1]);
    y += lengthdir_y(vitesse,aimtab[1]);
the draw_set_color in the middle of the code are for debug purpose
 
I don't understand your math, and I'm pretty sure there are more straightforward ways of doing that.

However, you would probably benefit from using the built-in function "angle_difference( angle1, angle2 )", which will return a value ALWAYS between -180 and 180. You can use that with the abs() function to get the absolute difference between two angles (measured in degrees).
 
G

graviax

Guest
thanks for the reply.
I should realy use built-in function instead of doing things my own...
thanks anyway
 
Top