Skip to main content
gamedev.net
🔒 Locked

simple angle question

Started by moon_rabbits 7 replies 700+ views
Original Post
moon_rabbits
moon_rabbits
I need help rotating a bitmap properly. I am using allegro to display graphics.
    float rx = canx-(CANNON_W/2)-mouse_x;
    float ry = cany-mouse_y;
    angle = atan(rx/ry)/(3.1415/180);
    
    angle = -angle;
That's what I have so far. It finds the distance of the mouse's x and y from the cannon's x and y respectively. After that, it determines the angle the mouse is making with the cannon and converts it to degrees. I use the function rotate_sprite when I am drawing the cannon:

    rotate_sprite(surface, cannon, int(canx), int(cany), ftofix(angle));
It blits the cannon bitmap to the bitmap 'surface', which is a buffer, at (canx, cany). ftofix(angle) rotates the bitmap according to angle before it is blitted. It works, but it is very buggy. It will jump and skip at parts, I assume this is because it is dividing by zero, am I right? Also, sometimes the cannon doesn't line up with the mouse perfectly. For instance, if the mouse is in the far top-right or top-left then the cannon's angle of inclination is too small for it to actually be properly lined up with the mouse. What can I do to keep the precision of the angle?
minyman22
minyman22
I've never used allegro, but I know that sometimes constant real-time rotation of a sprite can bog down a slow computer.
moon_rabbits
moon_rabbits
Yeah, it can get pretty slow on my computer. But the speed shouldn't affect the rotation, should it?
xissburg
xissburg
cant you create a rotation matrix by yourself in allegro? if yes, just do it:

| cos(theta) -sin(theta) |
| sin(theta) cos(theta) |
.
Zakwayda
Zakwayda
Quote:
float rx = canx-(CANNON_W/2)-mouse_x;
float ry = cany-mouse_y;
angle = atan(rx/ry)/(3.1415/180);
Given how most graphics systems are set up, I'd think you'd want:
float rx = canx+(CANNON_W/2)-mouse_x;
I'm not that familiar with Allegro though, so I could be wrong about this.

In any case, try using atan2(ry,rx) rather that atan(rx/ry) (assuming you have such a function available). This will at least address the 'divide by zero' problem.
xissburg
xissburg
rotations are done through rotations matrices. when you call that rotate_sprite() function, it takes the angle in the argument and throws into that rotation matrix I've shown above. Try searching for it . . . and pay attention: what would happen to your code if mouse_y = cany?!
.
moon_rabbits
moon_rabbits
I've tried all suggestions, but it is still rotating incorrectly.

I also noticed that when the mouse is all the way to the right or left, the cannon is almost completely horizontal, despite the fact that the mouse's y position is still above the cannon.
Zakwayda
Zakwayda
Quote:
Original post by moon_rabbits
I've tried all suggestions, but it is still rotating incorrectly.

I also noticed that when the mouse is all the way to the right or left, the cannon is almost completely horizontal, despite the fact that the mouse's y position is still above the cannon.
I don't have an answer, but a quick Google search suggests that the Allegro function rotate_sprite() may expect the angle in a form other than radians or degrees (e.g. in the range [0,256]), so you might double-check the API reference.

If that doesn't lead you to a solution, perhaps you could post your code in its current form so that we can take a look at it.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.