summaryrefslogtreecommitdiff
path: root/src/tesseract/OldParticle.java
blob: ca77acb8ffd81b107981dfe475cf903d69315210 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package tesseract;

import com.sun.j3d.utils.geometry.*;
import java.awt.*;
import javax.media.j3d.*;
import javax.vecmath.*;

import tesseract.objects.Forceable;

public class OldParticle implements Forceable {
	private float inverseMass;
	private Vector3f position, prevPosition;
	private Vector3f velocity;
	private Vector3f force;
	private BranchGroup BG;
	private TransformGroup TG;

	private static final float RADIUS = 0.004f;
	
	public OldParticle(Color3f color, Vector3f position, Vector3f velocity) {
		inverseMass = 1;
		this.position = new Vector3f(position);
		prevPosition = new Vector3f(position);
		this.velocity = new Vector3f(velocity);
		force = new Vector3f();
		TG = new TransformGroup();
		TG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		TG.addChild(createShape(color));
		BG = new BranchGroup();
		BG.setCapability(BranchGroup.ALLOW_DETACH);
		BG.addChild(TG);
		updateTransformGroup();
	}
	
	public float getInverseMass() {
		return inverseMass;
	}

	public Vector3f getPosition() {
		return position;
	}

	public Vector3f getPreviousPosition() {
		return prevPosition;
	}

	public Vector3f getVelocity() {
		return velocity;
	}

	public Group getGroup() {
		return BG;
	}
	
	public void detach() {
		BG.detach();
	}

	public void addForce(Vector3f force) {
		this.force.add(force);
	}

	public void updateState(float duration) {
		// The force vector now becomes the acceleration vector.
		force.scale(inverseMass);
		prevPosition.set(position);
		position.scaleAdd(duration, velocity, position);
		position.scaleAdd(duration * duration / 2, force, position);
		velocity.scaleAdd(duration, force, velocity);
		// The force vector is cleared.
		force.x = force.y = force.z = 0;
		updateTransformGroup();
	}

	public void updateTransformGroup() {
		Transform3D tmp = new Transform3D();
		tmp.setTranslation(position);
		TG.setTransform(tmp);
	}
	
	private Node createShape(Color3f color) {
		ColoringAttributes cAttr;
		if (color == null) {
			Color randomColor = Color.getHSBColor((float)Math.random(), 1, 1);
			color = new Color3f(randomColor);
		}
		cAttr = new ColoringAttributes(color, ColoringAttributes.FASTEST);
		Appearance appearance = new Appearance();
		appearance.setColoringAttributes(cAttr);
		return new Sphere(RADIUS, 0, 8, appearance);
	}
}