01: /*
02: * $Header: /cvs/j3dfly/J3dFly/src/org/jdesktop/j3dfly/utils/vpbehaviors/vehicles/SimpleVehicle.java,v 1.1 2005/04/20 21:05:18 paulby Exp $
03: *
04: * Sun Public License Notice
05: *
06: * The contents of this file are subject to the Sun Public License Version
07: * 1.0 (the "License"). You may not use this file except in compliance with
08: * the License. A copy of the License is available at http://www.sun.com/
09: *
10: * The Original Code is Java 3D(tm) Fly Through.
11: * The Initial Developer of the Original Code is Paul Byrne.
12: * Portions created by Paul Byrne are Copyright (C) 2002.
13: * All Rights Reserved.
14: *
15: * Contributor(s): Paul Byrne.
16: *
17: **/
18: package org.jdesktop.j3dfly.utils.vpbehaviors.vehicles;
19:
20: import javax.media.j3d.TransformGroup;
21: import javax.media.j3d.Transform3D;
22: import javax.vecmath.Vector3f;
23: import javax.vecmath.Matrix3f;
24: import javax.vecmath.Vector3d;
25: import javax.vecmath.Point3d;
26: import com.sun.j3d.utils.geometry.Cylinder;
27: import com.sun.j3d.utils.picking.PickTool;
28: import com.sun.j3d.utils.picking.PickResult;
29:
30: /**
31: *
32: * A simple 6 wheeled vehicle
33: *
34: * @author paulby
35: * @version
36: */
37: public class SimpleVehicle extends javax.media.j3d.BranchGroup {
38:
39: private TransformGroup wheelTG[] = new TransformGroup[6];
40: private Vector3f[] wheelPos = new Vector3f[6];
41:
42: private Point3d rayEnd = new Point3d();
43: private Point3d rayStart = new Point3d();
44: private Transform3D t3d = new Transform3D();
45: private Matrix3f orient = new Matrix3f();
46:
47: private static final float WHEEL_RADIUS = 0.3f;
48:
49: /** Creates new SimpleVehicle */
50: public SimpleVehicle() {
51: super ();
52:
53: this .setPickable(false);
54:
55: wheelPos[0] = new Vector3f(-0.5f, WHEEL_RADIUS, 0.8f); // Front Left
56: wheelPos[1] = new Vector3f(-0.5f, WHEEL_RADIUS, 0f); // Middle Left
57: wheelPos[2] = new Vector3f(-0.5f, WHEEL_RADIUS, -0.8f); // Back Left
58: wheelPos[3] = new Vector3f(0.5f, WHEEL_RADIUS, 0.8f); // Front Right
59: wheelPos[4] = new Vector3f(0.5f, WHEEL_RADIUS, 0f); // Middle Right
60: wheelPos[5] = new Vector3f(0.5f, WHEEL_RADIUS, -0.8f); // Back Right
61:
62: orient.rotZ((float) Math.toRadians(90));
63:
64: for (int i = 0; i < 6; i++) {
65: t3d.set(orient, wheelPos[i], 1f);
66: wheelTG[i] = new TransformGroup(t3d);
67: wheelTG[i]
68: .setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
69: wheelTG[i].addChild(new Cylinder(WHEEL_RADIUS, 0.05f));
70: this .addChild(wheelTG[i]);
71: }
72: }
73:
74: public void vehicleMoved(PickTool pickTool, Transform3D location) {
75:
76: for (int i = 0; i < 6; i++) {
77: rayStart.set(wheelPos[i]);
78: rayStart.y += 1f;
79: rayEnd.set(wheelPos[i]);
80: rayEnd.y -= 3f;
81: location.transform(rayStart);
82: location.transform(rayEnd);
83:
84: pickTool.setShapeSegment(rayStart, rayEnd);
85: PickResult floor = pickTool.pickClosest();
86: if (floor != null) {
87: wheelPos[i].y = (float) floor.getIntersection(0)
88: .getPointCoordinates().y;
89: }
90:
91: t3d.set(orient, wheelPos[i], 1);
92: wheelTG[i].setTransform(t3d);
93: }
94: }
95: }
|