Implement gravity and velocity vector.
[gravitysimulator.git] / src / edu / mit / ezyang / gravity / j3d / ArrowGroup.java
blob0959ee5b194e9a70c22c018c696af4ab4231fb4c
1 /*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5 package edu.mit.ezyang.gravity.j3d;
7 import com.sun.j3d.utils.geometry.Cylinder;
8 import javax.media.j3d.BranchGroup;
9 import javax.media.j3d.Transform3D;
10 import javax.media.j3d.TransformGroup;
11 import javax.vecmath.Matrix3f;
12 import javax.vecmath.Vector3f;
14 /**
16 * @author Edward Z. Yang <ezyang@mit.edu>
18 public class ArrowGroup extends BranchGroup {
19 public ArrowGroup(Vector3f velocity) {
20 Cylinder stem = new Cylinder(0.02f, velocity.length());
21 stem.getAppearance().getMaterial().setEmissiveColor(0.3f, 0.3f, 0.8f);
23 setCapability(BranchGroup.ALLOW_DETACH);
24 TransformGroup arrowTransformGroup = new TransformGroup();
25 Transform3D arrowTransform3D = new Transform3D();
27 Vector3f translation = new Vector3f();
28 translation.scale(1f/2f, velocity);
30 Vector3f vec_y = (Vector3f) velocity.clone();
31 vec_y.normalize();
33 Vector3f vec_x; // reference vector, will correct later
34 if (vec_y.x == 0 && vec_y.z == 0) {
35 vec_x = new Vector3f(-vec_y.y, 0f, 0f); // could be optimized
36 } else {
37 vec_x = new Vector3f(0f, 1f, 0f);
40 Vector3f vec_z = new Vector3f();
41 vec_z.cross(vec_x, vec_y);
42 vec_z.normalize();
44 vec_x.cross(vec_z, vec_y);
45 vec_x.normalize();
46 vec_x.negate();
48 Matrix3f rotation = new Matrix3f(
49 vec_x.x, vec_x.y, vec_x.z,
50 vec_y.x, vec_y.y, vec_y.z,
51 vec_z.x, vec_z.y, vec_z.z
53 rotation.invert();
55 arrowTransform3D.set(rotation, translation, 1f);
57 arrowTransformGroup.setTransform(arrowTransform3D);
58 arrowTransformGroup.addChild(stem);
59 addChild(arrowTransformGroup);