2d rectangle moving around a circle

Started by
2 comments, last by mnansgar 19 years, 4 months ago
can anyone help me with the width,height offset of revolving a rectangle around a circle. What i mean is I am revolving a rectangle around a game entity in a circle this rectangle is a bullet for a gun so for my revolution firex = cos(fireangle) * entityradius + entity_centerx; firey = sin(fireangle) * entityradius + entity_centery; the problem is that with my blt I draw from top left corner so if i shoot up or left too much the bullet rectangle hits my entity. I can fix this with some ifs but then the bullet jumps up and down with a small angle change. I have had some partial success with massive trig formulas and bullet widths and heights but overall nothing seems to work. Anyways I'm sure this is something easy but I'v spent a week on it and have no solution, so any help would be apreciated. Thanks Robert. [Edited by - robert_p on December 1, 2004 3:35:52 PM]
Advertisement
radians not degrees

cos(fireangle) doesnt compute correctly because the computer cos / sin functions use radians rather than degrees.

Rad(theta) = theta * pi / 180

I am using radians/deg correcly. The problem is that if i am rotating a rectangle around the circle and I blt from top left.
So the right side will look like this     *      This is supposed to be a circle =/    * *   *   *[--]    * *     * but for left     *         * *   [--]*    * *     * 

same problem for top and bottom.

I think someting like offseting the x,y of the bullet based of sin, cos funcions multiplied by some part of bullet width height. This got me like 2 correct corners or something, I can't really remeber because I've tried so much weird stuff to get this to work and I'm not on the computer with my engine on it.

[Edited by - robert_p on December 1, 2004 4:14:15 PM]
You probably want to:
(1) Make the circle radius half of the sum of the bounding-box diagonal lengths of the image and entity, and
(2) Render the image such that its center follows the circle instead of its top left edge.

This guarantees that the image will not occlude the player bitmap, regardless of how large the bullet bounding box is. You can see this is a guarantee because you can position the bullet box diagonal to the entity box, then the line from the center of the entity to the center of the image is the maximal distance just calculated (the sum of the two diagonals if both boxes have the same width:length ratios, so really this is an approximation that will never occlude, but won't always be exact) -- note that the hypotenuse is always longer than either side of a right triangle.

Thus in brief:

(1)
newradius = 0.5 * (sqrt(entity_width*entity_width + entity_height*entity_height) + sqrt(image_width*image_width + image_height*image_height));

(2)
firex = (cos(fireangle) * newradius + entity_centerx) - image_width/2;
firey = (sin(fireangle) * newradius + entity_centery) - image_height/2;

I hope this is what you're looking for! If you'd like any more clarification, please reply!

[Edited by - mnansgar on December 1, 2004 8:12:35 PM]
h20, member of WFG 0 A.D.

This topic is closed to new replies.

Advertisement