Radar for 3D Space Games

I found creating a radar for a 6DOF game quite difficult and there were few resources that I could find online that gave me an idea of what to do, so I’m posting this here in hopes that someone will find this useful.



	private void renderRadar(C3DVideo v) {
		int radius = 75;
		float sensitivity = 100; // the higher the sensitivity, the smoother the rotations are of the dots being drawn on the radar
		float sens_rad = radius * sensitivity;
		int dotRadius = 2;
		int xl = (mode.getWidth() / 2) - 200; // arbitrary circle drawing positions, you can put these where you'd like
		int yl = (mode.getHeight() / 2) + 120; // ^

		v.drawCircle(xl, yl, radius, 0xffffff); // draw circle at xl, yl with given radius and a white color

		v.drawCircle(xl - dotRadius, yl - dotRadius, dotRadius, 0x00ff00);
		
		// the following three lines of code should be modified to create no garbage, but this is up to you.
		float[] ps = new float[] { player.pos_x, player.pos_y, player.pos_z }; // put player position (x,y,z) into float array
		fPlane plt = new fPlane(ps, player.tangent.toArray()); // new plane, 'ps' is the point on the plane, tangent is  normal.
		fPlane plb = new fPlane(ps, player.binormal.toArray()); // new plane, 'ps' is the point on the plane, binormal is normal.
		// tangent vector is the 'forward' vector of the player, and the binormal is the 'left/right' vector of the player
		for (int i = 0; i < targets.size(); i++) {
			Entity e = targets.get(i);
			dotRadius = (int) e.radius / 8; // optional, but this line creates dots on the radar that are scaled so that they reflect the radius of the enemies
			float d1 = plb.pointPlaneDistance(e.pos_x, e.pos_y, e.pos_z); // find distance from entity's position to the binormal plane.
			if (d1 > sens_rad) {
				d1 = sens_rad;
			}
			if (d1 < -sens_rad) {
				d1 = -sens_rad;
			}
			float d2 = plt.pointPlaneDistance(e.pos_x, e.pos_y, e.pos_z); // find distance from entity's position to the tangent plane.
			if (d2 > sens_rad) {
				d2 = sens_rad;
			}
			if (d2 < -sens_rad) {
				d2 = -sens_rad;
			}
			double xc = d1 / sensitivity;
			double yc = -d2 / sensitivity; // negate the y coordinate because of window graphics
			double mag = Math.sqrt(xc * xc + yc * yc);
			// Scale vector to meet the radius if the magnitude of the vector is larger than the radius
			if (mag > radius) {
				xc  /= mag;
				yc /= mag;
				xc *= radius;
				yc *= radius;
			}
			v.drawCircle(xl + (int) xc - dotRadius, yl + (int) yc - dotRadius, dotRadius, 0xff0000); // draw entity dot on radar
		}
	}


Picture of it in game