Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (710) Games in Android Showcase (212) games submitted by our members Games in WIP (784) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Home Help Search Login Register
Pages: [1]
 ignore  |  Print
 rotating a point around a circle  (Read 5180 times) 0 Members and 1 Guest are viewing this topic.
weston
 « Posted 2004-12-03 02:29:40 »

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.

for(int i = 1; i > 0; i++)
System.out.println(i+" cups of java downed");
Malohkan

Senior Devvie

while (true) System.out.println("WOO!!!!");

 « Reply #1 - Posted 2004-12-03 03:08:15 »

 1  2 `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.

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
weston
 « Reply #2 - Posted 2004-12-03 03:55:44 »

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

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?

for(int i = 1; i > 0; i++)
System.out.println(i+" cups of java downed");
 Games published by our own members! Check 'em out!
weston
 « Reply #3 - Posted 2004-12-03 04:23:19 »

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.

for(int i = 1; i > 0; i++)
System.out.println(i+" cups of java downed");
weston
 « Reply #4 - Posted 2004-12-08 20:55:43 »

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

for(int i = 1; i > 0; i++)
System.out.println(i+" cups of java downed");
Malohkan

Senior Devvie

while (true) System.out.println("WOO!!!!");

 « Reply #5 - Posted 2004-12-08 23:08:16 »

try something like this:
 1  2 `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;`

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
weston
 « Reply #6 - Posted 2004-12-09 03:45:59 »

Hum, nope.

for(int i = 1; i > 0; i++)
System.out.println(i+" cups of java downed");
Malohkan

Senior Devvie

while (true) System.out.println("WOO!!!!");

 « Reply #7 - Posted 2004-12-09 11:13:57 »

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

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
weston
 « Reply #8 - Posted 2004-12-09 16:41:31 »

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.

for(int i = 1; i > 0; i++)
System.out.println(i+" cups of java downed");
Malohkan

Senior Devvie

while (true) System.out.println("WOO!!!!");

 « Reply #9 - Posted 2004-12-09 19:28:41 »

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.

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
 Games published by our own members! Check 'em out!
weston
 « Reply #10 - Posted 2004-12-09 19:56:48 »

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

for(int i = 1; i > 0; i++)
System.out.println(i+" cups of java downed");
weston
 « Reply #11 - Posted 2004-12-10 02:16:09 »

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

for(int i = 1; i > 0; i++)
System.out.println(i+" cups of java downed");
Malohkan

Senior Devvie

while (true) System.out.println("WOO!!!!");

 « Reply #12 - Posted 2004-12-10 04:30:45 »

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  I'm not really sure what to try right now... I'll keep pondering!

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 numerical (103 views) 2017-02-21 07:32:16 numerical (103 views) 2017-02-21 07:31:46 theagentd (206 views) 2017-02-18 13:42:33 theagentd (211 views) 2017-02-18 13:35:16 h.pernpeintner (1372 views) 2017-01-24 22:39:11 h.pernpeintner (1360 views) 2017-01-24 22:38:32 Galdo (1922 views) 2017-01-12 13:44:09 Archive (1997 views) 2017-01-02 05:31:41 0AndrewShepherd0 (2533 views) 2016-12-16 03:58:39 0AndrewShepherd0 (2316 views) 2016-12-15 21:50:57
 SkyAphid 38x philfrei 27x theagentd 26x Archive 25x basil_ 21x princec 21x Spasi 21x misthalu 20x Coldstream24 16x Aalhex 15x orange451 14x wessles 13x dime26 11x Riven 10x SHC 9x KaiHH 9x
 List of Learning Resourcesby elect2016-09-09 09:47:55List of Learning Resourcesby elect2016-09-08 09:47:20List of Learning Resourcesby elect2016-09-08 09:46:51List of Learning Resourcesby elect2016-09-08 09:46:27List of Learning Resourcesby elect2016-09-08 09:45:41List of Learning Resourcesby elect2016-09-08 08:39:20List of Learning Resourcesby elect2016-09-08 08:38:19Rendering resourcesby Roquen2016-08-08 05:55:21
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org