Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (408)
games submitted by our members
Games in WIP (293)
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  
  !! Please help !! Java2d geometry question.  (Read 1859 times)
0 Members and 1 Guest are viewing this topic.
Offline Lappa

Junior Newbie





« Posted 2008-04-08 15:55:39 »

Hi! I have a java2d geometry problem that I am trying to solve for a project I'm currently working on. My maths isn't great by any stretch of the imagination, so it might be true that this is a lot simpler than I am imagining. Please forgive my ignorance. I have made a quick visual representation of what I am trying to achieve (see). The scenario is as follows:

My (circular) bot has a surrounding Circle representing it's sensor range. When this circle intercepts an obstacle (also a circle) my checkCollision() method returns true. The Bot class holds the bearing (in radians) which the bot is currently travelling towards. When the bot collides with the Obstacle, I want it to call the rotate() method - this rotates the bot by a small increment each game loop - until the bot is facing in the opposite direction to the Obstacle. I create a line to join the obstacle with the bot, representing the bearing of the Obstacle. But what I really need to know is theta - the angle between the current bearing and the new bearing.

My first idea was to call rotate() until the bearing line was parallel with the obstBearing line, however I can't work out how to do that and it might cause a problem if the bot rotates clockwise instead of anti-clockwise. So it looks like I'm in for some complicated trigonometry. Any ideas(?) much appreciated. Thanks for reading this.

Here's some of my code to give an idea of how my program works:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
public double calculateBearing() {
      double newBearing = 0;
      Line hypotenuse;
      Vector2f intersection;
      double m, c, x2, x1, y2, y1, r;
     
      x1 = botCentre.x;
      y1 = botCentre.y;
      x2 = obstLoc.x;
      y2 = obstLoc.y;
      r = sensCircle.getRadius();
      m = (botCentre.y - obstLoc.y)/(botCentre.x - obstLoc.y); // gradient (or slope) of line
     c = y1 - (m * x1); // y intersection

//calculations here
     
      return newBearing;
   }

Offline ryanm
« League of Dukes »

Senior Member


Projects: 1


Used to be bleb


« Reply #1 - Posted 2008-04-08 16:27:48 »

I'm assuming you're using vecmath, based on the names in the diagram:

1  
2  
3  
4  
5  
Vector2f desiredDir = new Vector2f( x1 - x2, y1 - y2 );
Vector2f botDir = new Vector2f( Math.cos( botBearing ), Math.sin( botBearing) );

double theta = desiredDir.angle( botDir );
theta *= Line2D.relativeCCW( 0, 0, botDir.x, botDir.y, desiredDirx, desiredDir.y );


Untested, so that final multiplication might need to be negated but it'll be easy to spot if that's the case.

edit: premature posting
Offline Lappa

Junior Newbie





« Reply #2 - Posted 2008-04-09 15:10:46 »

Thanks for your help. I found out that it is in fact a lot simpler than I thought. By using arctan on the change in x and y I get the correct result:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
   public void calculateBearing() {
     
      updateObstacleLine();
     
      float dx = (botCentre.x - obstLoc.x);
      float dy = (botCentre.y - obstLoc.y);
     
      double thetaRadians = (StrictMath.atan2(dy,dx));
      double thetaDegrees = StrictMath.toDegrees(thetaRadians);
     
      targetAngle = thetaRadians;
     
      targetBearingVec = new Vector2f(thetaDegrees);

   }


However, I now need to work out how to decide whether to rotate CW or CCW to get to this new bearing. Many thanks for your input.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ryanm
« League of Dukes »

Senior Member


Projects: 1


Used to be bleb


« Reply #3 - Posted 2008-04-09 18:04:45 »

However, I now need to work out how to decide whether to rotate CW or CCW to get to this new bearing. Many thanks for your input.

Check out Line2D.relativeCCW for this.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars and Titan!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (123 views)
2013-05-17 21:29:12

alaslipknot (131 views)
2013-05-16 21:24:48

gouessej (161 views)
2013-05-16 00:53:38

gouessej (153 views)
2013-05-16 00:17:58

theagentd (166 views)
2013-05-15 15:01:13

theagentd (151 views)
2013-05-15 15:00:54

StreetDoggy (195 views)
2013-05-14 15:56:26

kutucuk (219 views)
2013-05-12 17:10:36

kutucuk (218 views)
2013-05-12 15:36:09

UnluckyDevil (221 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.147 seconds with 21 queries.