Hi guys,
I'm pretty new to the Java3D stuff and I've encountered a very strange problem...
I wanted to make a very simple application which moves a car around and the
user can control it with the keyboard. I also wanted to position the viewpoint behind
the car. I made my own Behavior class which reacts to the keyevents by updating
the speed and the wheel angle of the car and it also stimulated by time intervals when
it calculates the new position of the car. It's something like this:
class MyBehavior extends Behavior {
private TransformGroup target = null;
private WakeupCondition criteria = null;
private long dt = 50;
private Vector3d defOrient = new Vector3d(1, 0, 0); //the default orientation
of the car
private double angle = 0; //the starting angle of the car to the X axis
private Vector3d up = new Vector3d(0.0, 1.0, 0.0);
public MyBehavior(TransformGroup tg) {
target = tg;
WakeupCriterion[] wcarray = new WakeupCriterion[2];
wcarray[0] = new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED);
wcarray[1] = new WakeupOnElapsedTime(dt);
criteria = new WakeupOr(wcarray);
}
public void initialize() {
wakeupOn(criteria);
}
public void processStimulus(Enumeration e) {
while (e.hasMoreElements()) {
WakeupCondition condition = (WakeupCondition)e.nextElement();
if (condition instanceof WakeupOnAWTEvent) {
AWTEvent[] events = ((WakeupOnAWTEvent)condition).getAWTEvent();
for (int i = 0; i < events.length; i++) {
if (events[i] instanceof KeyEvent) {
int code = ( (KeyEvent) events[i]).getKeyCode();
if ( code == KeyEvent.VK_UP && speed <= 5) {speed += 0.01;}
if ( code == KeyEvent.VK_DOWN && speed >= -3) {speed -=
0.01;}
if ( code == KeyEvent.VK_LEFT && speed != 0 && wheel >=
-0.1) {wheel -= 0.05;} //it only turns the wheel when the car moves
if ( code == KeyEvent.VK_RIGHT && speed != 0 && wheel <=
0.1) {wheel += 0.05;}
} //if
} //for
}//if AWT event
else if (condition instanceof WakeupOnElapsedTime) {
double ds = (speed * dt);
double dx = ds * Math.cos(angle);
double dz = ds * Math.sin(angle);
double dangle = speed * Math.tan(wheel);
angle += dangle;
if (angle > 2*Math.PI) {angle -= 2*Math.PI;}
Matrix3d rotm = new Matrix3d(Math.cos(dangle), 0,
-Math.sin(dangle),
0,
1, 0,
Math.sin(dangle), 0,
Math.cos(dangle));
Transform3D tr = new Transform3D();
Matrix3d orig = new Matrix3d();
Vector3d move = new Vector3d();
target.getTransform(tr);
tr.get(move); //retreiving the translation of the car
tr.get(orig); //retreiving the rotation of the car
orig.mul(rotm);
rotm.transform(orient); //updating the orientation of the car
(rotating the orig vector)
move.x += dx;
move.z += dz;
location.set(move); //updating the location of the car
tr.set(orig); //updating the rotation component of the transform
tr.setTranslation(move); //updating the translation component of
the transform
target.setTransform(tr);
// updating the view
TransformGroup vpTR = simpleU.getViewingPlatform().
getViewPlatformTransform();
Transform3D viewT3D = new Transform3D();
Point3d camera = new Point3d(location);
camera.sub(orient);
viewT3D.lookAt(camera, location, up);
viewT3D.invert();
vpTR.setTransform(viewT3D);
}//else if time condition
}//while
wakeupOn(criteria);
}//processStimulus
}//MyBehavior
The strange is that when I place the camera to the location of the car (before moving
it a bit back)
then only the camera moves! If I replace the line: Point3d camera = new
Point3d(location);
with: Point3d camera = new Point3d();
then the car moves perfectly and the camera stands still in thestarting position and
looks to the car.
Does anybody know why on earth the car doesn't move when I try to follow it with the
camera?
And sorry if I wrote something ridiculous but I've been trying to find it out really
hard but I couldn't solve it...
Thanx for any help.
Miklos
==========================================================================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".