Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

this is a graphics/math problem. Consider this 2 sprite / surface

alt text

alt text

As you can see they are a tank and his gun turret. Now, i manage the turret rotation, and it works, and, also, the rotation of the tank on the screen.

What it'snt work is the correct "blitting" of the turret on my tank.

The problem is that the turret is never blitted correctly on the tank. This is a math problem, and i'm not so good in math ! How can i correct blit the turret centered on the "right" hole of my tank sprite ?

I'm using c# and sdl.net and actually i blit the turret with this code ( 13,13 are the X and Y position of the 'black' hole of the tank, but, i know, this is not the right method to blit it!):

            Surface tmpsurf = new Surface("graphics/tank_turret_long.png");

            Surface turretSurf = tmpsurf.CreateRotatedSurface(cannonangle);

            turretSurf.Transparent = true;

            turretSurf.TransparentColor = Color.Black;                

            Point posTurret = new Point(13+this.X,13+this.Y);
            Video.Screen.Blit(torrettaSurf,posTorretta); 

Thank in advance

share|improve this question
add comment

1 Answer

Your problem seems to be that you're blitting the turret with respect to its top left corner, when you actually want to blit it with respect to the center of the actual turret. Now, assuming your turret has its center at x_1, y_1, rotating it clockwise by theta degrees should give you a "new" center at x_2=x_1*cos(theta), y_2=y_1*sin(theta). You then have to line this up with the center of the turret hole, located at 13,13. This shouldn't be a problem (keep in mind I'm not very versed in C#, so this is probably syntactically incorrect):

Point posTurret = new Point(13 + this.X - x1*Math.cos(cannonangle),
                            13 + this.Y - y1*Math.sin(cannonangle));

Where x1 and y1 are the coordinates of the center of the turret in "tank_turret_long.png".

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.