Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (753) Games in Android Showcase (228) games submitted by our members Games in WIP (842) 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 1659 times) 0 Members and 1 Guest are viewing this topic.
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, returnif(!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 beamPoints = new ArrayList();//Step allows me to reduce the number points I test when it finally worksint step = 1;for(int n=0;n verts = new ArrayList();   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

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
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;`
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!
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;}`

Jimmt
« League of Dukes »

JGO Kernel

Medals: 167
Projects: 5
Exp: 6 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

 nelsongames (5 views) 2018-04-24 18:15:36 nelsongames (5 views) 2018-04-24 18:14:32 ivj94 (586 views) 2018-03-24 14:47:39 ivj94 (49 views) 2018-03-24 14:46:31 ivj94 (383 views) 2018-03-24 14:43:53 Solater (63 views) 2018-03-17 05:04:08 nelsongames (110 views) 2018-03-05 17:56:34 Gornova (159 views) 2018-03-02 22:15:33 buddyBro (705 views) 2018-02-28 16:59:18 buddyBro (93 views) 2018-02-28 16:45:17
 KaiHH 13x SHC 10x ByerN 10x NuclearPixels 10x Zemlaynin 10x Guerra2442 6x Damocles 6x VaTTeRGeR 5x ags1 4x Spasi 4x orangepascal 4x philfrei 4x mesterh 3x princec 3x ndnwarrior15 3x Springrbua 2x
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 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