com.badlogic.gdx.math.Intersector Java Examples

The following examples show how to use com.badlogic.gdx.math.Intersector. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NavMesh.java    From GdxDemo3D with Apache License 2.0 6 votes vote down vote up
/**
 * Make a ray test at this point, using a ray spanning from far up in the sky, to far down in the ground,
 * along the up axis.
 *
 * @param testPoint        The test point
 * @param out              The point of intersection between ray and triangle
 * @param allowedMeshParts Which mesh parts to test. Null if all meshparts should be tested.
 * @return The triangle, or null if ray did not hit any triangles.
 */
public Triangle verticalRayTest(Vector3 testPoint, Vector3 out, Bits allowedMeshParts) {
	tmpRayVerticalRayTest.set(
			tmpVerticalRayTest1.set(Constants.V3_UP).scl(500).add(testPoint),
			tmpVerticalRayTest2.set(Constants.V3_DOWN));
	Triangle hitTri = rayTest(tmpRayVerticalRayTest, 1000, allowedMeshParts);
	if (hitTri == null) {
		// TODO: Perhaps this should be Nan?
		out.set(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY);
		return null;
	} else {

		Intersector.intersectRayTriangle(tmpRayVerticalRayTest, hitTri.a, hitTri.b, hitTri.c, out);
		return hitTri;
	}
}
 
Example #2
Source File: Collision.java    From libgdx-demo-pax-britannica with MIT License 6 votes vote down vote up
private static void collisionCheck(Bullet bullet, Ship ship) {
	if (bullet.id!=ship.id && ship.alive) {
		
		for(int i = 0; i<ship.collisionPoints.size;++i) {
			if(Intersector.isPointInPolygon(bullet.collisionPoints, ship.collisionPoints.get(i))) {
				ship.damage(bullet.damage);
				GameInstance.getInstance().bulletHit(ship, bullet);
				bullet.alive = false;
				return;
			}
		}
		
		for(int i = 0; i<bullet.collisionPoints.size;++i) {
			if(Intersector.isPointInPolygon(ship.collisionPoints, bullet.collisionPoints.get(i))) {
				ship.damage(bullet.damage);
				GameInstance.getInstance().bulletHit(ship, bullet);
				bullet.alive = false;
				return;
			}
		}
	}
}
 
Example #3
Source File: GameScreen.java    From libgdx-demo-pax-britannica with MIT License 6 votes vote down vote up
@Override
public boolean touchDown(int x, int y, int pointer, int button) {
	collisionRay = cam.getPickRay(x, y);
	
	if(numPlayers >0 && Intersector.intersectRayBoundsFast(collisionRay, touchAreaP1) && GameInstance.getInstance().factorys.size>0) {
		((FactoryProduction) GameInstance.getInstance().factorys.get(0)).button_held = true;
		pointerP1 = pointer;
		touchedP1 = true;
	} 
	if(numPlayers >1 && Intersector.intersectRayBoundsFast(collisionRay, touchAreaP2) && GameInstance.getInstance().factorys.size>1) {
		((FactoryProduction) GameInstance.getInstance().factorys.get(1)).button_held = true;
		pointerP2 = pointer;
		touchedP2 = true;
	} 
	if(numPlayers >2 && Intersector.intersectRayBoundsFast(collisionRay, touchAreaP3) && GameInstance.getInstance().factorys.size>2) {
		((FactoryProduction) GameInstance.getInstance().factorys.get(2)).button_held = true;
		pointerP3 = pointer;
		touchedP3 = true;
	} 
	if(numPlayers >3 && Intersector.intersectRayBoundsFast(collisionRay, touchAreaP4) && GameInstance.getInstance().factorys.size>3) {
		((FactoryProduction) GameInstance.getInstance().factorys.get(3)).button_held = true;
		pointerP4 = pointer;
		touchedP4 = true;
	} 
	return false;
}
 
Example #4
Source File: GameEngine.java    From GdxDemo3D with Apache License 2.0 5 votes vote down vote up
@Override
public float addSingleResult(LocalRayResult rayResult, boolean normalInWorldSpace) {
	float hitFraction = rayResult.getHitFraction();
	btCollisionObject hitObj = rayResult.getCollisionObject();
	Entity entity = getEntity(hitObj.getUserPointer());

	if (entity instanceof GameModel) {
		GameModel model = (GameModel) entity;
		if (hitFraction < this.hitFraction && (layers == null || model.visibleOnLayers.intersects(layers))) {
			this.hitFraction = hitFraction;
			super.addSingleResult(rayResult, normalInWorldSpace);
			return hitFraction;
		}

	} else if (entity.getId() == scene.navmeshBody.getId()) {
		Triangle triangle = scene.navMesh.rayTest(ray, rayDistance, layers);
		if (triangle == null) {
			// Triangle is not on allowed layer
			return 1;
		}
		Intersector.intersectRayTriangle(ray, triangle.a, triangle.b, triangle.c, tmp);
		hitFraction = rayFrom.dst(tmp) / rayFrom.dst(rayTo);
		if (hitFraction < this.hitFraction) {
			this.hitFraction = hitFraction;
			rayResult.setHitFraction(hitFraction);
			super.addSingleResult(rayResult, normalInWorldSpace);
			return hitFraction;
		}
	}
	return 1;
}
 
Example #5
Source File: NavMesh.java    From GdxDemo3D with Apache License 2.0 5 votes vote down vote up
/**
 * Calculate a triangle graph path between two triangles which are intersected by the rays.
 *
 * @param fromRay
 * @param toRay
 * @param allowedMeshParts
 * @param distance
 * @param path
 * @return
 */
public boolean getPath(Ray fromRay, Ray toRay, Bits allowedMeshParts,
					   float distance, NavMeshGraphPath path) {

	Triangle fromTri = rayTest(fromRay, distance, allowedMeshParts);
	if (fromTri == null) {
		Gdx.app.debug(TAG, "From triangle not found.");
		return false;
	}
	Vector3 fromPoint = new Vector3();
	Intersector.intersectRayTriangle(fromRay, fromTri.a, fromTri.b, fromTri.c, fromPoint);

	return getPath(fromTri, fromPoint, toRay, allowedMeshParts, distance, path);
}
 
Example #6
Source File: NavMesh.java    From GdxDemo3D with Apache License 2.0 5 votes vote down vote up
/**
 * Calculate a triangle graph path from a start triangle to the triangle which is intersected by a ray.
 *
 * @param fromTri
 * @param fromPoint
 * @param toRay
 * @param allowedMeshParts
 * @param distance
 * @param path
 * @return
 */
public boolean getPath(Triangle fromTri, Vector3 fromPoint, Ray toRay, Bits allowedMeshParts,
					   float distance, NavMeshGraphPath path) {
	Triangle toTri = rayTest(toRay, distance, allowedMeshParts);
	if (toTri == null) {
		Gdx.app.debug(TAG, "To triangle not found.");
		return false;
	}
	Vector3 toPoint = new Vector3();
	Intersector.intersectRayTriangle(toRay, toTri.a, toTri.b, toTri.c, toPoint);

	return getPath(fromTri, fromPoint, toTri, toPoint, path);
}
 
Example #7
Source File: WorldBodyUtils.java    From killingspree with MIT License 5 votes vote down vote up
public void destroyEntities(ServerBomb bomb, float radius, Vector2 position) {
    Body body = bomb.body;
    circle.set(position, radius);
    for (ServerEntity entity: worldManager.entities) {
        if (entity.body == body || entity.body.toDestroy) {
            continue;
        }
        if (Intersector.overlaps(circle, entity.body.rectangle)) {
            Vector2 step = entity.body.getPosition();
            float length = position.dst(step);
            step.sub(position);
            float max = Math.max(step.x, step.y);
            step.scl(4 / max);
            Body otherBody = Ray.findBody(world,
                    body, step, length, true);
            if (otherBody == null) {
                if (entity instanceof LivingCategory) {
                    if (((LivingCategory)entity.body.getUserData()).kill()) {
                        if (bomb.bomber != entity.body.getUserData())
                            bomb.bomber.addKill();
                        else {
                            bomb.bomber.reduceKill();
                        }
                    }
                }
            }
        }
    }
}
 
Example #8
Source File: Help.java    From libgdx-demo-pax-britannica with MIT License 5 votes vote down vote up
@Override
public boolean touchDown(int x, int y, int pointer, int button) {
	collisionRay = cam.getPickRay(x, y);
	
	if (Intersector.intersectRayBoundsFast(collisionRay, collisionBack)) {
		finished = true;
	}
	return false;
}
 
Example #9
Source File: Stage3D.java    From gdx-vr with Apache License 2.0 5 votes vote down vote up
@Override
public Vector2 screenToStageCoordinates(Vector2 screenCoords) {
	Ray pickRay = getViewport().getPickRay(screenCoords.x, screenCoords.y);
	Vector3 intersection = tmp;
	if (Intersector.intersectRayPlane(pickRay, plane, intersection)) {
		screenCoords.x = intersection.x;
		screenCoords.y = intersection.y;
	} else {
		screenCoords.x = Float.MAX_VALUE;
		screenCoords.y = Float.MAX_VALUE;
	}
	return screenCoords;
}
 
Example #10
Source File: NavMeshPointPath.java    From GdxDemo3D with Apache License 2.0 4 votes vote down vote up
/**
 * Calculate the shortest path through the navigation mesh triangles.
 *
 * @param trianglePath
 */
public void calculateForGraphPath(NavMeshGraphPath trianglePath) {
	clear();
	nodes = trianglePath.nodes;
	this.start = new Vector3(trianglePath.start);
	this.end = new Vector3(trianglePath.end);
	this.startTri = trianglePath.startTri;

	// Check that the start point is actually inside the start triangle, if not, project it to the closest
	// triangle edge. Otherwise the funnel calculation might generate spurious path segments.
	Ray ray = new Ray(tmp1.set(V3_UP).scl(1000).add(start), tmp2.set(V3_DOWN));
	if (!Intersector.intersectRayTriangle(ray, startTri.a, startTri.b, startTri.c, null)) {
		float minDst = Float.POSITIVE_INFINITY;
		Vector3 projection = new Vector3();
		Vector3 newStart = new Vector3();
		float dst;
		// A-B
		if ((dst = GeometryUtils.nearestSegmentPointSquareDistance(projection, startTri.a, startTri.b, start)) < minDst) {
			minDst = dst;
			newStart.set(projection);
		}
		// B-C
		if ((dst = GeometryUtils.nearestSegmentPointSquareDistance(projection, startTri.b, startTri.c, start)) < minDst) {
			minDst = dst;
			newStart.set(projection);
		}
		// C-A
		if ((dst = GeometryUtils.nearestSegmentPointSquareDistance(projection, startTri.c, startTri.a, start)) < minDst) {
			minDst = dst;
			newStart.set(projection);
		}
		start.set(newStart);
	}
	if (nodes.size == 0) {
		addPoint(start, startTri);
		addPoint(end, startTri);
	} else {
		lastEdge = new Edge(nodes.get(nodes.size - 1).getToNode(), nodes.get(nodes.size - 1).getToNode(), end, end);
		calculateEdgePoints();
	}
}
 
Example #11
Source File: NavMeshPointPath.java    From GdxDemo3D with Apache License 2.0 4 votes vote down vote up
/**
 * Store all edge crossing points between the start and end indices.
 * If the path crosses exactly the start or end points (which is quite likely),
 * store the edges in order of crossing in the EdgePoint data structure.
 * <p/>
 * Edge crossings are calculated as intersections with the plane from the
 * start, end and up vectors.
 *
 * @param startIndex
 * @param endIndex
 * @param startPoint
 * @param endPoint
 */
private void calculateEdgeCrossings(int startIndex, int endIndex,
									Vector3 startPoint, Vector3 endPoint) {

	if (startIndex >= numEdges() || endIndex >= numEdges()) {
		return;
	}
	crossingPlane.set(startPoint, tmp1.set(startPoint).add(V3_UP), endPoint);

	EdgePoint previousLast = lastPointAdded;

	Edge edge = getEdge(endIndex);
	EdgePoint end = new EdgePoint(new Vector3(endPoint), edge.toNode);

	for (int i = startIndex; i < endIndex; i++) {
		edge = getEdge(i);

		if (edge.rightVertex.equals(startPoint) || edge.leftVertex.equals(startPoint)) {
			previousLast.toNode = edge.toNode;
			if (!previousLast.connectingEdges.contains(edge, true)) {
				previousLast.connectingEdges.add(edge);
			}

		} else if (edge.leftVertex.equals(endPoint) || edge.rightVertex.equals(endPoint)) {
			if (!end.connectingEdges.contains(edge, true)) {
				end.connectingEdges.add(edge);
			}

		} else if (Intersector.intersectSegmentPlane(edge.leftVertex, edge.rightVertex, crossingPlane, tmp1)
				&& !Float.isNaN(tmp1.x + tmp1.y + tmp1.z)) {
			if (i != startIndex || i == 0) {
				lastPointAdded.toNode = edge.fromNode;
				EdgePoint crossing = new EdgePoint(new Vector3(tmp1), edge.toNode);
				crossing.connectingEdges.add(edge);
				addPoint(crossing);
			}
		}
	}
	if (endIndex < numEdges() - 1) {
		end.connectingEdges.add(getEdge(endIndex));
	}
	if (!lastPointAdded.equals(end)) {
		addPoint(end);
	}
}
 
Example #12
Source File: MainMenu.java    From libgdx-demo-pax-britannica with MIT License 4 votes vote down vote up
@Override
public boolean touchDown(int x, int y, int pointer, int button) {

	collisionRay = cam.getPickRay(x, y);
	
	if (cnt > 4 || countdown.finished)
		return false;	
	
	// check if ship is activated
	if (Intersector.intersectRayBoundsFast(collisionRay, p1.collision) && !p1.picked) {
		p1.picked = true;
	} else if (Intersector.intersectRayBoundsFast(collisionRay, p2.collision) && !p2.picked) {
		p2.picked = true;
	} else if (Intersector.intersectRayBoundsFast(collisionRay, p3.collision) && !p3.picked) {
		p3.picked = true;
	} else if (Intersector.intersectRayBoundsFast(collisionRay, p4.collision) && !p4.picked) {
		p4.picked = true;
	} else if (Intersector.intersectRayBoundsFast(collisionRay, p1.collisionPlayerSelect) && p1.picked && !p1.cpuSelect) {
		p1.playerSelect = true;
		p1.cpuSelect = false;
	} else if (Intersector.intersectRayBoundsFast(collisionRay, p2.collisionPlayerSelect) && p2.picked && !p2.cpuSelect) {
		p2.playerSelect = true;
		p2.cpuSelect = false;
	} else if (Intersector.intersectRayBoundsFast(collisionRay, p3.collisionPlayerSelect) && p3.picked && !p3.cpuSelect) {
		p3.playerSelect = true;
		p3.cpuSelect = false;
	} else if (Intersector.intersectRayBoundsFast(collisionRay, p4.collisionPlayerSelect) && p4.picked && !p4.cpuSelect) {
		p4.playerSelect = true;
		p4.cpuSelect = false;
	} else if (Intersector.intersectRayBoundsFast(collisionRay, p1.collisionCPUSelect) && p1.picked && !p1.playerSelect) {
		p1.cpuSelect = true;
		p1.playerSelect = false;
	} else if (Intersector.intersectRayBoundsFast(collisionRay, p2.collisionCPUSelect) && p2.picked && !p2.playerSelect) {
		p2.cpuSelect = true;
		p2.playerSelect = false;
	} else if (Intersector.intersectRayBoundsFast(collisionRay, p3.collisionCPUSelect) && p3.picked && !p3.playerSelect) {
		p3.cpuSelect = true;
		p3.playerSelect = false;
	} else if (Intersector.intersectRayBoundsFast(collisionRay, p4.collisionCPUSelect) && p4.picked && !p4.playerSelect) {
		p4.cpuSelect = true;
		p4.playerSelect = false;
	}
	
	if (Intersector.intersectRayBoundsFast(collisionRay, collisionMusic)) {
		if (cnt >= 1)
			return false;
		Preferences prefs = Gdx.app.getPreferences("paxbritannica");
		prefs.putBoolean("music", !prefs.getBoolean("music"));
		prefs.flush();
		if(prefs.getBoolean("music")) {
			if(Resources.getInstance().music == null) Resources.getInstance().reInit();
			if(!Resources.getInstance().music.isPlaying()) { 
				Resources.getInstance().music.play();
				Resources.getInstance().music.setLooping(true);
			}
			musicOnOff.setColor(1,1,1,0.5f);				
		} else {
			Resources.getInstance().music.stop();
			musicOnOff.setColor(1,1,1,0.1f);
		}			
	}
	
	if (Intersector.intersectRayBoundsFast(collisionRay, collisionHelp)) {
		if (cnt >= 1)
			return false;
		countdown.finished = true;
		changeToScreen = 0;
	}
	
	if (Intersector.intersectRayBoundsFast(collisionRay, collisionSettings)) {
		if (cnt >= 1)
			return false;
		countdown.finished = true;
		changeToScreen = 1;
	}
	
	return false;
}
 
Example #13
Source File: PolygonUtils.java    From bladecoder-adventure-engine with Apache License 2.0 4 votes vote down vote up
/**
 * Clamp the point to the nearest polygon segment
 * 
 * @param poly
 *            The polygon
 * @param x
 *            The original point X
 * @param y
 *            The original point Y
 * @param dest
 *            The clamped point
 * @return The segment where the clamped point belongs
 */
public static int getClampedPoint(Polygon poly, float x, float y,
		Vector2 dest) {
	float verts[] = poly.getTransformedVertices();
	float dTmp;

	Intersector.nearestSegmentPoint(verts[0], verts[1], verts[2], verts[3],
			x, y, dest);

	int nearest = 0;
	float d = Vector2.dst(x, y, dest.x, dest.y);

	for (int i = 2; i < verts.length; i += 2) {
		Intersector.nearestSegmentPoint(verts[i], verts[i + 1],
				verts[(i + 2) % verts.length],
				verts[(i + 3) % verts.length], x, y, tmp);
		dTmp = Vector2.dst(x, y, tmp.x, tmp.y);

		if (dTmp < d) {
			d = dTmp;
			nearest = i;
			dest.set(tmp);
		}
	}
	
	// ERROR CONTROL:
	// If the clamped point is not in the walkzone 
	// we search for the nearest walkzone vertex
	if (!PolygonUtils.isPointInside(poly, dest.x, dest.y, true)) {
		EngineLogger.debug("> PolygonalPathFinder: CLAMPED FAILED!!");
		
		tmp.set(verts[0], verts[1]);
		d = Vector2.dst(x, y, tmp.x, tmp.y);
		nearest = 0;
		dest.set(tmp);
		
		for (int i = 2; i < verts.length; i += 2) {
			tmp.set(verts[i], verts[i + 1]);
			dTmp = Vector2.dst(x, y, tmp.x, tmp.y);

			if (dTmp < d) {
				d = dTmp;
				nearest = i;
				dest.set(tmp);
			}
		}
	}

	return nearest;
}