rotating a point around a circle

I’ve had this problem a couple times before and I’ve been able to just figure out numbers that work, but now I would like to know the actual formula so I don’t have to mess around with it so much next time. The kind of thing I’m looking for is how to find a point on a circle depending on a certain rotation value. A good example of this is for finding the spawn point for a bullet on a ship that rotates (e.g. asteroids). If the ship is in the default position (facing up), the bullet just spawns at x = ship.x+ship.width/2, y = 0; The problem I’m having is in finding the correct x and y once the ship is rotated. My problem isn’t exactly like that since the object can only rotate along the left hand side of an imaginary circle, but its similar enough.

If you need more information, just let me know. Thanks for any help.

x = ship.x + Math.cos(theta)*ship.width/2;
y = ship.y + Math.sin(theta)*ship.width/2;

The width twice is correct. That’ll center it on the y, and move it to the front of the ship on its x, even if it’s rotated.

This is what the image looks like that I’m rotating:

http://www.cyntaks.com/projects/images/misc/example.png

it rotates from the point drawn on the image using:

g2.translate(x, y);
g2.rotate(rotation, sprite.width-10, 10);
//draw image at 0,0
g2.rotate(-rotation, sprite.width-10, 10);
g2.translate(-x, -y);

the projectile should leave the gun at 42px from the top of the arm image, and 5 from the left, so I’m doing something like this to get its spawn point:

float newX = (arm.x+5)+(float)(Math.cos(rotation)*(20f));

float newY = (arm.y+42)+(float)(Math.sin(rotation)*(20f));

This isn’t even close to working… I got the value 20 because the width of the arm image (which is greater than the height) is 50px, but I am rotating it 10px from the right so I figure the circular arc it makes has a 40px diameter or 20px radius. Any idea whats going on?

I uploaded a version that uses the code above if its any help to see whats going on: http://www.cyntaks.com/projects/christmasgame
up/down will do the rotation, spacebar fires, S will disable sound.

edit: I uploaded a new version that uses a slightly modified version of the code I posted above:

float newY = (42+arm.y) - (float)(Math.sin(rotation)*20f);

*it was adding before.

sorry to bother but I can’t seem to figure this out… could someone explain the principles of the math I’m trying to do here (or give me a link), or at least let me know of anything incorrect that stands out to you.

Even if I just do this:

newX = (arm.x)+(float)(Math.cos(rotation)((arm.width-10)/2));
newY = (arm.y)+(float)(Math.sin(rotation)
((arm.width-10)/2));

to simplify the math, I still can’t find values that will keep the snowball’s position constant relative to a point on the arm image. I don’t start my trig class for another 6 weeks, please don’t make me wait that long :stuck_out_tongue:

try something like this:


x = arm.x + Math.cos(theta)*42 + Math.cos(theta-Math.PI/2)*5; 
y = arm.y + Math.sin(theta)*42 + Math.sin(theta-Math.PI/2)*5;

Hum, nope. ???

hmmm maybe I don’t quite understand your problem. Check out Rimscape and see how the thrusters works. The ship rotates about its center, and the thrusters rotate about the ship’s center, but offset to different edges.

Here is my thruster code:
http://www.gamelizard.com/com/Thruster.java

I’ve done the same thing for my asteroids game, but the problem appears to be different when your not rotating around the center or something… I’ll upload some more code when I get home tonight.

well the math doesn’t care if your “center” is the literal center of your image. If instead of arm.x or arm.y being considered as the center, just use arm.x+42 and arm.y+5 as your coordinates (if that’s the right numbers) for the center.

I think I found the problem. I earlier I said that I thought the arc would have a radius of 20px, but I believe that is incorrect. The radius should be the distance from my point of rotation to the point where I want the snowball to appear (read a few posts back for how I was getting the radius before). Once I plug in the correct radius I believe it will work, but I’m still at school :wink:

Ok, still not working. Here is a summary of all the data involved:

point of rotation (on gun image): x:40, y:10
spawn point for snowball (on gun image): x:5, y:42

when the spawn point rotates around the point of rotation, a circle with radius: 35.355px is formed. I found that radius with this equation:
radius = sqrt((40-5)^2 + (10-42)^2).

This is what I’m using to get the spawn point in the current version:

spawnX = (float)((arm.x+40)-(Math.cos(rotation)(35f)));
spawnY = (float)((arm.y+10)-(Math.sin(rotation)
(35f)));

(I’ve tried using 35.355339059327376220042218105242f instead of 35f without a noticeable difference).

Using this, snowballs are still not spawning in the correct place. I uploaded a new version that uses the data above. In the new version the snowballs don’t move after you shoot them so that you can clearly see the spawn point. If you shoot snowballs at each position you can see the arc that is formed by the rotation.

edit: heres the link again: http://www.cyntaks.com/projects/christmasgame

hmmm… makes me wanna play with that code in Debug mode in Eclipse. It’s nice cause you can change numbers and formula’s on the fly :slight_smile: I’m not really sure what to try right now… I’ll keep pondering!