Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (574) Games in Android Showcase (154) games submitted by our members Games in WIP (620) 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 3339 times) 0 Members and 1 Guest are viewing this topic.
weston

Junior Devvie

 « 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

Junior Devvie

 « 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

Junior Devvie

 « 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

Junior Devvie

 « 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

Junior Devvie

 « 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

Junior Devvie

 « 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

Junior Devvie

 « 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

Junior Devvie

 « 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.

 Riven (27 views) 2015-04-16 10:48:47 Duke0200 (42 views) 2015-04-16 01:59:01 Fairy Tailz (32 views) 2015-04-14 20:13:12 Riven (33 views) 2015-04-12 21:36:37 bus hotdog (49 views) 2015-04-10 02:39:32 CopyableCougar4 (51 views) 2015-04-10 00:51:04 BurntPizza (51 views) 2015-04-06 22:06:58 ags1 (53 views) 2015-04-02 10:58:48 Riven (52 views) 2015-04-01 18:27:05 ags1 (69 views) 2015-03-31 10:55:12
 theagentd 26x BurntPizza 18x wessles 15x 65K 11x kingroka123 11x Rayvolution 11x alwex 10x KevinWorkman 9x kevglass 8x phu004 8x Hanksha 7x SHC 7x ra4king 7x Olo 7x Roquen 7x chrislo27 7x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 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