Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (546)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Re: Rotate Towards Point on: 2013-01-23 21:01:54
Solution

I've finally reached a perfect solution, although I can't say this code is as efficient as can be. The real problem ended up the different way rotations were being stored. For the entities rotation, it run counterclockwise 0-360. The result of atan2 gave me something running clockwise and -180-180. The key turned out to be converting to counterclockwise 0-360, after which it was a trivial solution, although code posted earlier on here helped out, particulary in ensuring there isn't jerky movement when the entity is pointing towards the ship out by only a fraction of a degree.

I'm not sure if this solution would apply to anyone else's code, but hopefully it could be of help to somebody. Either way, there is plenty of reading material on this post, and I'd like to thank everybody who contributed and helped solve my issue!

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
playerAngle = (float) (Math.toDegrees((MathUtils.atan2(player.y-y, player.x-x)))+270)%360;
shipRotation = Math.abs(rotation%360);
turnSpeed = 2;
angle = playerAngle-shipRotation;
         
   if(Math.abs(angle)<turnSpeed){
      rotation = playerAngle;
   }else if(angle<0){
      rotation -= turnSpeed;
   }else{
      rotation += turnSpeed;
   }
2  Game Development / Newbie & Debugging Questions / Re: Rotate Towards Point on: 2013-01-23 20:16:50
I'm really confused, sorry, but this is maths all beyond my ability. Could anybody clarify how to actually practically implement this, ideally with  minimal advanced vector manipulation (advanced for me)! Thanks
3  Game Development / Newbie & Debugging Questions / Re: Rotate Towards Point on: 2013-01-21 21:48:03
Just an offtopic question: Why the heck is it (y, x) and not the usual (x, y)?

That's just the order the atan2 function in java's math library wants them.
4  Game Development / Newbie & Debugging Questions / Re: Rotate Towards Point on: 2013-01-21 19:59:52
1  
playerAngle = (float) Math.toDegrees(Math.atan2(player.x-x, player.y - y));


This line is wrong. It should be

1  
playerAngle = (float) Math.toDegrees(Math.atan2(player.y-y, player.x - x));


Oh yeah, cheers.

I'm still confused on how to get this fully working though. Merely using playerAngle won't account for the entities current rotation, it'll only give the angle between two points. The entity will just spin infinitely as playerAngle will never change so long as it is static, despite it rotating. How do I factor in it's rotation?

Thanks
5  Game Development / Newbie & Debugging Questions / Re: Rotate Towards Point on: 2013-01-21 19:48:54
Ok, so currently I am at this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
playerAngle = (float) Math.toDegrees(Math.atan2(player.x-x, player.y - y));
turnSpeed = 2;
                 
if(Math.abs(playerAngle) < turnSpeed)
   {
      rotation = playerAngle;
   }
   else if(playerAngle > 0)
   {
      rotation -= turnSpeed;
   }
   else
   {
      rotation += turnSpeed;
   }


At the moment it seems I am only accounting for the angle between two co-ordinates, and not the current rotation of the entity. It's integrating that part that I'm finding most difficult.
6  Game Development / Newbie & Debugging Questions / Re: Rotate Towards Point on: 2013-01-21 19:40:54
1  
PlayerAngle = (float) Math.toDegrees(Math.atan2(player.x-x, player.y - y));


This will give me the angle between -180 and 180, I'm not sure what the need for the dot product is?

Edit:
Sorry, you said 'or', my bad!
7  Game Development / Newbie & Debugging Questions / Re: Rotate Towards Point on: 2013-01-21 19:31:25
Thanks for the help, I'm relatively new to Vector Maths, is this code looking somewhat right?

Written in the update method of an entity, trying to point towards the player.

1  
2  
3  
4  
Vector2 p1 = new Vector2(x/Math.abs(x),y/Math.abs(y));
Vector2 p2 = new Vector2(player.x/Math.abs(player.x),player.y/Math.abs(player.y));
         
float angle = (float) Math.acos(Math.toRadians(p1.dot(p2)));
8  Game Development / Newbie & Debugging Questions / Rotate Towards Point on: 2013-01-21 18:44:45
Hi,

I expect a very simple answer, but beyond me. I've done some searching but can't find anything.

I want to rotate a sprite at a constant rate a tick so it will try to face another sprite.

I can calculate the angle between them, it's dealing with the rotation increment/decrement when the angle crosses the 360 degrees mark to 0 degrees, messes up my code and I just can't think through it.

Many thanks for any help!
9  Game Development / Newbie & Debugging Questions / Re: Line Intersecting Polygon Not Working on: 2013-01-13 20:41:19
The laser is line not a ray, it has a direction, a start point and a length, it's a finite distance.
The geometry consists of line segments with start and end points (A list of vertices in order, from which I can deduce the points of the segments).

Would this affect your code in any way? I'm not particularly experienced in vector mathematics.

Thanks
10  Game Development / Newbie & Debugging Questions / Line Intersecting Polygon Not Working on: 2013-01-13 20:11:47
Hi There,

I'm writing a small space game using libgdx, the ship fires a laser, and I need to calculate when (if ever) it intersects a poly (asteroids). This is so I can stop the laser passing through the objects, and actually collide with them. My code seems to work some of the time, and completely fails at other points. I can't figure out what causes it to fail, I'm pretty sure it's not my poly definitions that are wrong, as sometimes it will work on an edge, other times not, there is no consistency.

I have checked the trace of my ray (strictly speaking a line) and it is spot on, so it's not that. Here is the code, I'll try to explain what I've done after.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
//Beam Collisions

//If the beam isn't being fired, return
if(!active) return;
   
//Storing all the points to be checked on the beam, I've checked the all seem accurate, and have draw a line between them and they match the laser.  
List<Vector2> beamPoints = new ArrayList<Vector2>();

//Step allows me to reduce the number points I test when it finally works
int step = 1;
for(int n=0;n<maxHeight;n+=step){
   
        //Rotation is the rotation of the ship. I add 90 to it so the angle of my vector is from the x-axis.
       //shipX is the world coords of the ship. x is the relative start point of the laser to the ship.
       //shipX refers to the centre of the ship, not the corner of the graphic.
  beamPoints.add(
         new Vector2(shipX+x+(float)(n*Math.cos(Math.toRadians(rotation+90))),
                  shipY+y+(float)(n*Math.sin(Math.toRadians(rotation+90)))));
}
   
//Here I cycle through the entities to test if they collide. Currently the only entities are asteroids.  
for(Entity e : entities){
        //Skip over the entity if it's outside the render range or it has no geometry.
  if(!e.inRenderRange || e.getGeometry()==null) continue;

        //A list to store all the vertices of that asteroids geometry.
  List<Vector2> verts = new ArrayList<Vector2>();
   for(Vector2 v : e.getGeometry()){
     
                //Determining the x and y of the vertice to store. e.x is the asteroids world coords, I subtract the half the imageWidth because it's position is stored as the centre of the graphic.
               //I then add the relative x and y of the vertex.
               //I've turned off rotation for the asteroids, so that's not a problem.
     float nx = (e.x-e.imageWidth/2)+v.x;
      float ny = (e.y-e.imageHeight/2)+v.y;
     
      verts.add(new Vector2(nx,ny));
     
   }
   
        //Testing each of the points on the beam to see if there are in the poly.
  for(int j=0;j<beamPoints.size();j++){
     
                //Using Intersector, a class in Libgdx, I can safely assume this is working fine.
     if(Intersector.isPointInPolygon(verts, beamPoints.get(j))){
         
                        //Changing the height (should be labelled length) of the beam to be that at which it collides. Step is one, so doesn't matter for now.
        height = j*step;
         break;
      }
   }
   
   
}


I should also point out from the following images that by the laser I mean the bright core, I haven't bothered messing around with it's glow, that's not faulty, I just haven't done it yet.




I hope this is enough code to go by, if there is anything else I can provide please ask; I'm greatful for whatever help you can give.

Cheers guys,

Daniel
Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

atombrot (20 views)
2014-08-19 09:29:53

Tekkerue (21 views)
2014-08-16 06:45:27

Tekkerue (21 views)
2014-08-16 06:22:17

Tekkerue (12 views)
2014-08-16 06:20:21

Tekkerue (19 views)
2014-08-16 06:12:11

Rayexar (55 views)
2014-08-11 02:49:23

BurntPizza (37 views)
2014-08-09 21:09:32

BurntPizza (27 views)
2014-08-08 02:01:56

Norakomi (35 views)
2014-08-06 19:49:38

BurntPizza (64 views)
2014-08-03 02:57:17
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!