KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > buchuki > ensmer > input > command > navigation > MoveCommand


1 /*
2  * Copyright 2004 Dusty Phillips
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package com.buchuki.ensmer.input.command.navigation;
18
19 import com.buchuki.ensmer.EnsmerManager;
20 import com.buchuki.ensmer.input.Accelerator;
21 import com.buchuki.ensmer.input.event.EnsmerInputEvent;
22 import com.buchuki.ensmer.input.event.Measurable;
23 import javax.media.j3d.Transform3D;
24 import javax.vecmath.*;
25
26 /**
27  *
28  * @author Dusty Phillips [dusty@buchuki.com]
29  */

30 public class MoveCommand extends TransformCommand {
31     
32     /**
33      * Creates a new MoveCommand with a reference to the transformable,
34      * accelerator, and modifier.
35      *
36      * @param transformable the Transformable to notify whenever the position
37      * is updated
38      * @param accel the Accelerator used to dynamically modify the magnitude
39      * @param accelMod a value to statically modify the magnitude (for hardware
40      * correction)
41      * @param axis a member of the Axis enumeration that specifies which axis
42      * to move in
43      */

44     public MoveCommand(Transformable3D transformable, Accelerator accel,
45     float accelMod, Axis axis) {
46         super(transformable, accel, accelMod);
47         this.axis = axis;
48     }
49     
50     /**
51      * Execute the command; in this case, create a movement vector on the
52      * axis the movement was constructed with, and move the object. Note that
53      * the vector is automatically transformed to be perpendicular to the user's
54      * current view
55      *
56      * @param event the event that was executed
57      */

58     public boolean execute(EnsmerInputEvent event) {
59         Transform3D transform = new Transform3D();
60         float[] vector = new float[3];
61         vector[axis.getIndex()] = getAcceleratedMagnitude((Measurable) event);
62         Vector3f vec = new Vector3f(vector);
63         transformVector(vec);
64         transform.set(vec);
65         Matrix4f matrix = new Matrix4f();
66         transform.get(matrix);
67         updatePosition(matrix);
68         return true;
69     }
70     
71     /**
72      * method to transform the vector. Subclasses should override this if they
73      * need to apply extra transformations (specifically, aligning it with the
74      * view) to the vector. The default implementation is empty.
75      *
76      * @param vector the Vector to be transformed
77      */

78     protected void transformVector(Vector3f vector) {
79     }
80     
81     
82     /**
83      * The axis associated with this MoveCommand
84      */

85     private Axis axis;
86     
87     /**
88      * Enumeration of three Axes in three dimensions. Used to simplify
89      * implementation details.
90      */

91     public enum Axis {
92         X(0),
93         Y(1),
94         Z(2);
95         
96         /**
97          * The array index associated with the axis in typical vectors
98          */

99         private int arrayIndex;
100         
101         /**
102          * Construct the enum with a reference to the ArrayIndex associated
103          * with the axis.
104          *
105          * @param index the Array Index usually associated with this Axis
106          */

107         Axis(int index) {
108             this.arrayIndex = index;
109         }
110         
111         /**
112          * Obtain a reference to the array index associated with the
113          * axis
114          */

115         int getIndex() {
116             return arrayIndex;
117         }
118     }
119 }
120
Popular Tags