Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
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 3003 times)
0 Members and 1 Guest are viewing this topic.
Offline weston

Junior Duke





« 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");
}
Offline Malohkan

Senior Duke




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!
Offline weston

Junior Duke





« 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!
Legends of Yore - The Casual Retro Roguelike
Offline weston

Junior Duke





« 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");
}
Offline weston

Junior Duke





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

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

Senior Duke




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!
Offline weston

Junior Duke





« Reply #6 - Posted 2004-12-09 03:45:59 »

Hum, nope. Huh

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

Senior Duke




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!
Offline weston

Junior Duke





« 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");
}
Offline Malohkan

Senior Duke




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!
Legends of Yore - The Casual Retro Roguelike
Offline weston

Junior Duke





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

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

Junior Duke





« 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");
}
Offline Malohkan

Senior Duke




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

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (46 views)
2014-10-17 03:59:02

Norakomi (37 views)
2014-10-16 15:22:06

Norakomi (29 views)
2014-10-16 15:20:20

lcass (32 views)
2014-10-15 16:18:58

TehJavaDev (62 views)
2014-10-14 00:39:48

TehJavaDev (62 views)
2014-10-14 00:35:47

TehJavaDev (52 views)
2014-10-14 00:32:37

BurntPizza (70 views)
2014-10-11 23:24:42

BurntPizza (40 views)
2014-10-11 23:10:45

BurntPizza (82 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!