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.
//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