Could someone take a quick look at the following class. The problem is
that when a rotation is performed and then a translation the translation
takes place on the local coordinates. i.e it moves the shape in the
direction of the rotation. I want the shape to be moved in the same
direction whatever the rotation. Any help would be much appreciated.
Thanks
Ben
public class MyPickTranslateBehavior extends PickTranslateBehavior {
protected WakeupCondition m_WakeupCondition = null;
private int x = 0;
private int y = 0;
private int x_last = 0;
private int y_last = 0;
private double y_angle = 0.0;
private double x_factor = 0.02;
int changeX;
int changeY;
private Transform3D modelTrans = null;
private Transform3D transformY = null;
private PickResult pickResult = null;
private Node pickNode = null;
private TransformGroup transformGroup = null;
Canvas3D canvas;
BranchGroup objRoot;
Java3DFrame frame;
float step = 0.005f; // was 0.01
public MyPickTranslateBehavior(Java3DFrame f, BranchGroup objRoot,
Canvas3D canvas,
BoundingSphere bounds) {
super(objRoot, canvas, bounds);
this.frame = f;
this.setBounds(bounds);
this.canvas = canvas;
this.objRoot = objRoot;
transformGroup = new TransformGroup();
modelTrans = new Transform3D();
transformY = new Transform3D();
transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
//create the wakeupCriterion for the behavior
WakeupCriterion criterionArray[] = new WakeupCriterion[2];
criterionArray[0] = new WakeupOnAWTEvent(MouseEvent.MOUSE_PRESSED);
criterionArray[1] = new WakeupOnAWTEvent(MouseEvent.MOUSE_DRAGGED);
//save the WakeupCriterion for the behavior
m_WakeupCondition = new WakeupOr(criterionArray);
}
public void initialize() { // initialize
wakeupOn(m_WakeupCondition); //Apply the initial WakeupCriterion
}
public void processStimulus(Enumeration criteria) { // processStimulus
while (criteria.hasMoreElements()) {
WakeupCriterion wakeUp = (WakeupCriterion) criteria.nextElement();
if (wakeUp instanceof WakeupOnAWTEvent) {
AWTEvent[] evt = ( (WakeupOnAWTEvent) wakeUp).getAWTEvent();
try {
processAWTEvent(evt);
}
catch (Exception e) {
}
}
}
//Assign the next WakeupCondition, so we are notified again
wakeupOn(m_WakeupCondition);
}
private void processAWTEvent(AWTEvent[] events) {
for (int n = 0; n < events.length; n++) {
if (events[n] instanceof MouseEvent) {
MouseEvent eventPress = (MouseEvent) events[n];
if (eventPress.getModifiers() == MouseEvent.BUTTON1_MASK) {
//System.out.println("LEFT MOUSE PRESSED _ DOING ROTATION");
if (eventPress.getID() == MouseEvent.MOUSE_PRESSED) {
pickCanvas.setShapeLocation(eventPress);
pickResult = pickCanvas.pickClosest();
if (pickResult != null) {
pickNode = pickResult.getObject();
transformGroup = (TransformGroup)
pickResult.getNode(PickResult.
TRANSFORM_GROUP);
//System.out.println("DELETE MODE? " +
frame.deleteMode());
if (frame.deleteMode() == true) { // So delete that shape
try {
//no capability to detact the branchgroup
transformGroup.removeAllChildren(); // Not yet working
}
catch (Exception ex) {
System.out.println("DELETE EXCEPTION : " +
ex.toString());
}
}
else { // Setup rotation of that shape
transformGroup.getTransform(modelTrans);
x = x_last = eventPress.getX();
}
}
}
if (eventPress.getID() == MouseEvent.MOUSE_DRAGGED) {
// Rotating the object around the Y axis only
x = eventPress.getX();
y_angle = (x - x_last) * x_factor;
transformY.rotY(y_angle);
modelTrans.mul(modelTrans, transformY);
transformGroup.setTransform(modelTrans);
x_last = x;
}
}
if (eventPress.getModifiers() == MouseEvent.BUTTON3_MASK) {
//System.out.println("RIGHT MOUSE PRESSED _ DOING
TRANSLATION");
if (eventPress.getID() == MouseEvent.MOUSE_PRESSED) {
pickCanvas.setShapeLocation(eventPress);
pickResult = pickCanvas.pickClosest();
if (pickResult != null) {
pickNode = pickResult.getObject();
transformGroup = (TransformGroup) pickResult.getNode(
PickResult.
TRANSFORM_GROUP);
transformGroup.getTransform(modelTrans);
x = x_last = eventPress.getX();
y = y_last = eventPress.getY();
}
}
if ( (eventPress.getID() == MouseEvent.MOUSE_DRAGGED)) {
// Translating the object on both the X and Y axis
x = eventPress.getX();
y = eventPress.getY();
changeX = x - x_last;
changeY = y - y_last;
/*
transformY.set(new Vector3f( ( (float) factor * (float)
changeX),
0f,
(float) factor * (float)
changeY));
modelTrans.mul(modelTrans, transformY);
transformGroup.setTransform(modelTrans);
*/
updateScene();
x_last = x;
y_last = y;
}
}
}
}
}
public void updateScene() {
int loopCount = changeX;
if (changeX < 0) {
loopCount = changeX * -1; //make pos
}
loopCount = loopCount * 2;
boolean rightCollision = false;
boolean leftCollision = false;
int stepCountRight = 0;
int stepCountLeft = 0;
for (int i = 0; i < loopCount; i++) {
if (frame.inCollision() && (leftCollision == false) &&
(rightCollision == false)) {
if (changeX < 0) {
transformY.set(new Vector3f(step, 0f, 0f));
leftCollision = true;
}
else {
transformY.set(new Vector3f(-step, 0f, 0f));
rightCollision = true;
}
}
else {
if (changeX > 0) {
if ((rightCollision == true) && (stepCountRight == 0)) {
transformY.set(new Vector3f(-step, 0f, 0f));
stepCountRight--;
}
else {
transformY.set(new Vector3f(step, 0f, 0f));
}
if(rightCollision && (stepCountRight != 0)){
stepCountRight++;
}
}
else {
if ((leftCollision == true) && (stepCountLeft == 0)) {
transformY.set(new Vector3f(step, 0f, 0f));
stepCountLeft--;
}
else {
transformY.set(new Vector3f(-step, 0f, 0f));
}
if(leftCollision && (stepCountLeft != 0)){
stepCountLeft++;
}
}
}
modelTrans.mul(modelTrans, transformY);
transformGroup.setTransform(modelTrans);
}
}
}
********************************************
Ben Logan - Graduate Software Engineer
RCID
Stephenson Building
Newcastle Upon Tyne
NE1 7RU
www.rcid.ncl.ac.uk
********************************************
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the
body
of the message "signoff JAVA3D-INTEREST". For general help, send email
to
[EMAIL PROTECTED] and include in the body of the message "help".