summaryrefslogtreecommitdiff
path: root/src/tesseract/objects/Cannon.java
blob: c1ddce6f322ca5e2de10736d93c5234646c58837 (plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package tesseract.objects;

import javax.media.j3d.IndexedQuadArray;
import javax.media.j3d.PointArray;
import javax.media.j3d.Transform3D;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;

public class Cannon extends PhysicalObject{

	public Cannon(Vector3f thePosition, float mass) {
		super(thePosition, mass);
		// TODO Auto-generated constructor stub
		buildShape(14, 7f, 2f, 2f);
	}
	
	private void buildShape(final int theDivisions, final float length,
			final float breechRadius, final float gunThickness) {
		Point3f[] coordinates = new Point3f[theDivisions * 4];
		final float barrelRadius = breechRadius + gunThickness;
		final float arcAngle = (float) (Math.PI * 2.0);
		final float gunDivAngle = 2 * (float) Math.PI / theDivisions;
		Transform3D center = new Transform3D();
		
		for (int i = 0; i < theDivisions; i++) {
			coordinates[i] = new Point3f(breechRadius
					* (float) Math.cos(i * gunDivAngle), 0f, breechRadius 
					* (float) Math.sin(i * gunDivAngle));
		}
		
		for (int i = theDivisions; i < theDivisions * 2; i++) {
			coordinates[i] = new Point3f(breechRadius
					* (float) Math.cos(i * gunDivAngle), length, breechRadius 
					* (float) Math.sin(i * gunDivAngle));
		}
		
		for (int i = theDivisions * 2; i < theDivisions * 3; i++) {
			coordinates[i] = new Point3f(barrelRadius
					* (float) Math.cos(i * gunDivAngle), 0f, barrelRadius 
					* (float) Math.sin(i * gunDivAngle));
		}
		
		for (int i = theDivisions * 3; i < theDivisions * 4; i++) {
			coordinates[i] = new Point3f(barrelRadius
					* (float) Math.cos(i * gunDivAngle), length, barrelRadius 
					* (float) Math.sin(i * gunDivAngle));
		}
		
		IndexedQuadArray geometry = new IndexedQuadArray(theDivisions * 4, PointArray.COORDINATES, 
				theDivisions * 4);
		int topInsideStop = 0;
		int index = 0;
		for (int i = 0; i < theDivisions; i++) {
			geometry.setCoordinate(index++, coordinates[i]);
			geometry.setCoordinate(index++, coordinates[(theDivisions + i) % (2 * theDivisions)]);
			geometry.setCoordinate(index++, coordinates[(theDivisions + i + 1) % (2 * theDivisions)]);
			geometry.setCoordinate(index++, coordinates[(i + 1) % theDivisions]);
		}
		
		for (int i = theDivisions * 2; i < theDivisions * 3; i++) {
			geometry.setCoordinate(index++, coordinates[i]);
			geometry.setCoordinate(index++, coordinates[(theDivisions + i) % (4 * theDivisions)]);
			geometry.setCoordinate(index++, coordinates[(theDivisions + i + 1) % (4 * theDivisions)]);
			geometry.setCoordinate(index++, coordinates[((i + 1) % theDivisions) + theDivisions * 2]);
		}
		
		for (int i = 0; i < theDivisions; i++) {
			geometry.setCoordinate(index++, coordinates[i]);
			geometry.setCoordinate(index++, coordinates[i + 3 * theDivisions]);
			geometry.setCoordinate(index++, coordinates[(1 + i + 3 * theDivisions) % theDivisions]);
			geometry.setCoordinate(index++, coordinates[(i + 1) % theDivisions]);
		}
	}
}