Jump to content
  • Advertisement
Sign in to follow this  
Akill

Finding a point on a rotated sprite

This topic is 5416 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, im new :) now i just know that when i look back into this thread ive probably been flamed in more ways than possible but heck, if i dont figure this out soon i will probably commit suicide anyway. Im using allegro :), im new to making games, this is my secound. Its just a simple 2d 'drive your tank and shoot your friend' game. Ive got them to drive fine, and ive got them to fire... its just- not out of the tanks cannon ^^; as i expected it to, it comes out of the bmps co-ordinates i need to find a formula to find the point of the cannon on my bmp, originally i thought of finding the centre by diving the height and width by 2 and going from there, but the bmp isnt a square. and because ive got my tanks rotating i have no idea how to get the point i want. if any1 knows any tutorials or anything of help to me that would be gr8, thx! and may you have mercy on my soul, flame wise :s

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
http://www.icarusindie.com/DoItYourSelf/rtsr/javascript3d/lesson2.php

It's in javascript but the math is the same and the language is easy enough to understand if the tutorial isn't clear enough.

I think that's what you need to know. You know the original location of the cannon point (same as in the BMP), that tutorial covers the rotation math that you need to figure out where that point is as it's rotating around your player's position.

You then use that unit vector (from the center of rotation to the cannon) to figure out the component velocities for your bullet so that it moves in the proper direction.

Share this post


Link to post
Share on other sites
to place a point local to a sprite (meaning, inside the sprite texture) into the world (meaning, on screen), it's


Vector Psprite_centre = Vector(sprite_width/2, sprite_height/2);
Vector dPlocal = (Plocal - Psprite_centre);
Vector dPworld = RotatePoint(dPLocal, sprite_angle));
Pworld = Psprite_pos + dPworld;

Vector RotatePoint(Vector Point, float angle)
{
float co = cos(angle);
float si = sin(angle);
Vector P = Point;
P.x = Point.x * ( co) + Point.y * (si);
P.y = Point.x * (-si) + Point.y * (co);

return P;
}



this is assuming the centre of rotation is the centre of te sprite (Psprite_centre).

Psprite_pos is the position of the centre of the sprite on screen.

the rest should be straight forward.

as an sidenote, to do the reverse (convert a sprite from screen into the texture), you simply do the exact reverse process.


Vector dPworld = Pworld - Psprite_pos;
Vector dPlocal = RotatePoint(dPworld, -sprite_angle);
Vector Psprite_centre = Vector(sprite_width/2, sprite_height/2);
Vector Plocal = dPlocal + Psprite_centre;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!