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
| if(!active) return; List<Vector2> beamPoints = new ArrayList<Vector2>();
int step = 1; for(int n=0;n<maxHeight;n+=step){ beamPoints.add( new Vector2(shipX+x+(float)(n*Math.cos(Math.toRadians(rotation+90))), shipY+y+(float)(n*Math.sin(Math.toRadians(rotation+90))))); } for(Entity e : entities){ if(!e.inRenderRange || e.getGeometry()==null) continue;
List<Vector2> verts = new ArrayList<Vector2>(); for(Vector2 v : e.getGeometry()){ float nx = (e.x-e.imageWidth/2)+v.x; float ny = (e.y-e.imageHeight/2)+v.y; verts.add(new Vector2(nx,ny)); } for(int j=0;j<beamPoints.size();j++){ if(Intersector.isPointInPolygon(verts, beamPoints.get(j))){ 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