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   
Pages: [1]
  ignore  |  Print  
  Line Intersecting Polygon Not Working  (Read 543 times)
0 Members and 1 Guest are viewing this topic.
Offline danielcmessias

Senior Newbie





« Posted 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
Offline Danny02
« Reply #1 - Posted 2013-01-13 20:37:03 »

to fix your problem, you shouldn't test laser-points, but just if the line of the laser is intersecting the triangles.
you would do it like this.

the laser is a half-line(?) so has a starting point and a direction.
the geometry you want to check against has a bunch of line-segments(start&end point or starting point + direction + length)

then just check if the half-line of the laser intersects any of the segments.

half-line intersection test with segment:
1  
2  
3  
4  
5  
6  
7  
8  
9  
Halfline laser;
Segment geoPart;

if(abs(laser.dir dot geoPart) != 1)//dot=vector dot product; check if not parallel and are because of that intersecting
{
  //get intersection point and check if infront of laser(dot product > 0) and is in the segment
 //sry to lazy to write it for you^^
}
return false;
Offline danielcmessias

Senior Newbie





« Reply #2 - Posted 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
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Danny02
« Reply #3 - Posted 2013-01-13 22:10:49 »

so your laser is a line-segment then? one start and end point.
a geometrie is build from line-segments.
you just have to check for each line-segment of your geometry if it intersects with the line-segment of the laser.
So just write a line-segment class, which has a "boolean intersects(LineSegment other)" method. Get a Iterable<LineSegments> from your geometry and check
1  
2  
3  
4  
for(LineSegment s: geometry.getSegments())
{
  if(s.intersects(laser.getSegment())) return true;
}


Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #4 - Posted 2013-01-14 04:46:28 »

Off topic, but I'd recommend using Actors and Stages, so that you can just call actor.hit() and other built in methods, it also provides the ability to draw/update all actors thru the stage. If you're interested take a look: http://code.google.com/p/libgdx-users/wiki/scene2d#Actor
http://code.google.com/p/libgdx-users/wiki/scene2d#Stage
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.

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 (28 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!