Skip to main content

Java 3D Key Rotation

1 reply [Last post]
Kaito-Hacker
Offline
Joined: 2011-03-23
Points: 0

The following code makes a object rotates on y-axis when any key is pressed. Is there way to make it so it only rotates when specified key is press? For example using navigation keys to rotate the cube, so with specified key pressed, the object will rotate on a specific axis and a specifid direction.
// initialize the Behavior
// set initial wakeup condition
// called when behavior beacomes live
public void initialize(){
// set initial wakeup condition
this.wakeupOn(new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED));
}

// behave
// called by Java 3D when appropriate stimulus occures
public void processStimulus(Enumeration criteria){
// decode event

// do what is necessary
angle += 0.1;
rotation.rotY(angle);
targetTG.setTransform(rotation);
this.wakeupOn(new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED));
}

Is it okay to do public void processStimulus(Enumeration criteria, KeyEvent e), and the result will be the same?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
harshadbits
Offline
Joined: 2010-12-10
Points: 0

Instead of creating a new object for WakeupOnAWTEvent, you can use a reference:
WakeupOnAWTEvent AWTEventCondition = new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED);
now, in the processStimulus(Enumeration criteria) function:
if(criterion.equals(AWTEventCondition)) {
AWTEvent[] event = ((WakeupOnAWTEvent)criterion).getAWTEvent();
for(int i=0; i<event.length; i++) {
if(event[i] instanceof KeyEvent) {
int keyCode = ((KeyEvent)event[i]).getKeyCode(); //retrieve key code
}
}
}
you can check if a specific key is pressed by comparing the key code with codes defined in KeyEvent class. Eg. KeyEvent.VK_E for "e" key.