How to change a car's starting position and heading(rotation)?

I am building a traffic simulation and still struggling with some basic things. The problem I am facing now is being able to change the starting position of a car and where it is looking at. I have been using the TestFancyCar example.



At first the example spawns the car at the origin above the ground.



Car above ground



Using the instruction carNode.move(0, -5, 0); I could move it closer to the ground.



Car closer to the ground



Now I have noticed that any actions have to be done before physics is attached otherwise the move falls on deaf bits.



Now the problem then becomes how to rotate the car so it starts looking at the right direction. Reading the wiki in https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:math it tells of using:



[java]carNode.getLocalRotation().fromAngles(x,y,z)[/java]



I do that and then the wheels have a fit of demonic posession and end up like this:



Demonic Wheels



My understanding was that once all nodes where attached one could rotate them at the same time. But reading through forum posts:



http://hub.jmonkeyengine.org/groups/general-2/forum/topic/rotate-a-wheel-of-the-car

http://hub.jmonkeyengine.org/groups/general-2/forum/topic/question-about-rotations/

http://hub.jmonkeyengine.org/groups/general-2/forum/topic/various-types-of-transforms/

http://hub.jmonkeyengine.org/groups/graphics/forum/topic/rotating-models/

http://hub.jmonkeyengine.org/groups/physics/forum/topic/rotating-a-dynamicphysicsnode/

http://hub.jmonkeyengine.org/groups/general-2/forum/topic/need-for-speed-car-choosing/

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:vehicles

http://hub.jmonkeyengine.org/groups/general-2/forum/topic/car-wheel-rotation-problem/

http://hub.jmonkeyengine.org/groups/general-2/forum/topic/rotating-cars-body/

http://hub.jmonkeyengine.org/groups/graphics/forum/topic/sphere-rotation-around-y-axis



They say that the problem is with the junction. It seems once I rotate the car the junctions lost they way. So I rotated them also but now they seem to have flying car syndrome:

[java]wheel_fr.center().getLocalRotation().fromAngles(0, 2, 0);[/java]

http://i843.photobucket.com/albums/zz359/thordivdev/Simulation_Project_Screenshots/Car_2012_08_06_D.png



The problem then, is should I change the values where I am changing them or should I change them later for easier handling? How can I try to achieve what I want.



[java]private void buildPlayer() {

float stiffness = 120.0f;//200=f1 car

float compValue = 0.2f; //(lower than damp!)

float dampValue = 0.3f;

final float mass = 400;



//Load model and get chassis Geometry

carNode = (Node)assetManager.loadModel(“Models/Ferrari/Car.scene”);

carNode.setShadowMode(ShadowMode.Cast);



//if Move is what i need to get it spawn where i Want

carNode.move(0, -5, 0);

carNode.getLocalRotation().fromAngles(0, 2, 0);



//carNode.scale(2);







Geometry chasis = findGeom(carNode, “Car”);

BoundingBox box = (BoundingBox) chasis.getModelBound();



//Create a hull collision shape for the chassis

CollisionShape carHull = CollisionShapeFactory.createDynamicMeshShape(chasis);



//Create a vehicle control

player = new VehicleControl(carHull, mass);

carNode.addControl(player);





//Setting default values for wheels

player.setSuspensionCompression(compValue * 2.0f * FastMath.sqrt(stiffness));

player.setSuspensionDamping(dampValue * 2.0f * FastMath.sqrt(stiffness));

player.setSuspensionStiffness(stiffness);

player.setMaxSuspensionForce(10000);



//Create four wheels and add them at their locations

//note that our fancy car actually goes backwards…

Vector3f wheelDirection = new Vector3f(0, -1, 0);

Vector3f wheelAxle = new Vector3f(-1, 0, 0);



Geometry wheel_fr = findGeom(carNode, “WheelFrontRight”);

//Original

//wheel_fr.center()

wheel_fr.center().getLocalRotation().fromAngles(0, 2, 0);

box = (BoundingBox) wheel_fr.getModelBound();

wheelRadius = box.getYExtent();

float back_wheel_h = (wheelRadius * 1.7f) - 1f;

float front_wheel_h = (wheelRadius * 1.9f) - 1f;

player.addWheel(wheel_fr.getParent(), box.getCenter().add(0, -front_wheel_h, 0),

wheelDirection, wheelAxle, 0.2f, wheelRadius, true);



Geometry wheel_fl = findGeom(carNode, “WheelFrontLeft”);

//wheel_fl.center();

wheel_fl.center().getLocalRotation().fromAngles(0, 2, 0);

box = (BoundingBox) wheel_fl.getModelBound();

player.addWheel(wheel_fl.getParent(), box.getCenter().add(0, -front_wheel_h, 0),

wheelDirection, wheelAxle, 0.2f, wheelRadius, true);



Geometry wheel_br = findGeom(carNode, “WheelBackRight”);

//wheel_br.center();

wheel_br.center().getLocalRotation().fromAngles(0, 2, 0);

box = (BoundingBox) wheel_br.getModelBound();

player.addWheel(wheel_br.getParent(), box.getCenter().add(0, -back_wheel_h, 0),

wheelDirection, wheelAxle, 0.2f, wheelRadius, false);



Geometry wheel_bl = findGeom(carNode, “WheelBackLeft”);

//wheel_bl.center();

wheel_bl.center().getLocalRotation().fromAngles(0, 2, 0);

box = (BoundingBox) wheel_bl.getModelBound();

player.addWheel(wheel_bl.getParent(), box.getCenter().add(0, -back_wheel_h, 0),

wheelDirection, wheelAxle, 0.2f, wheelRadius, false);



player.getWheel(2).setFrictionSlip(4);

player.getWheel(3).setFrictionSlip(4);



rootNode.attachChild(carNode);



getPhysicsSpace().add(player);





}[/java]

Have you tried setPhysicsLocation()?

You mix physics and node logic. The vehicle puts the wheels in the correct world location, there is no relation via nodes.

Normen you are very correct, I am mixing the physics and node logic. Looking at scene for dummies if I rotate one node all child nodes/spatials should rotate with it. There is no mention of physics doing the same.



Momoko Fan: No, I have not tried setPhysicsLocation.



After many combination and tries.

Doing wheel_fr.getParent().getLocalRotation().fromAngles(angles); will not work.



What worked for me was adding the physics to the node at the end of the function instead of the start.



[java]private void buildPlayer() {

float stiffness = 120.0f;//200=f1 car

float compValue = 0.2f; //(lower than damp!)

float dampValue = 0.3f;

final float mass = 400;



//Load model and get chassis Geometry

carNode = (Node)assetManager.loadModel("Models/Ferrari/Car.scene");

carNode.setShadowMode(ShadowMode.Cast);



Geometry chasis = findGeom(carNode, "Car");

BoundingBox box = (BoundingBox) chasis.getModelBound();



//Create a hull collision shape for the chassis

CollisionShape carHull = CollisionShapeFactory.createDynamicMeshShape(chasis);



//Create a vehicle control

player = new VehicleControl(carHull, mass);

//move the addControl(player Vehicle Control) from here

//carNode.addControl(player);





//Setting default values for wheels

player.setSuspensionCompression(compValue * 2.0f * FastMath.sqrt(stiffness));

player.setSuspensionDamping(dampValue * 2.0f * FastMath.sqrt(stiffness));

player.setSuspensionStiffness(stiffness);

player.setMaxSuspensionForce(10000);



//Create four wheels and add them at their locations

//note that our fancy car actually goes backwards…

Vector3f wheelDirection = new Vector3f(0, -1, 0);

Vector3f wheelAxle = new Vector3f(-1, 0, 0);



float angles[] = new float[] {0, 2, 0};



Geometry wheel_fr = findGeom(carNode, "WheelFrontRight");

wheel_fr.center();



//wheel_fr.getParent().getLocalRotation().fromAngles(angles);

box = (BoundingBox) wheel_fr.getModelBound();

wheelRadius = box.getYExtent();

System.out.println("Wheel parent is " + wheel_fr.getParent());

float back_wheel_h = (wheelRadius * 1.7f) - 1f;

float front_wheel_h = (wheelRadius * 1.9f) - 1f;

player.addWheel(wheel_fr.getParent(), box.getCenter().add(0, -front_wheel_h, 0),

wheelDirection, wheelAxle, 0.2f, wheelRadius, true);



Geometry wheel_fl = findGeom(carNode, "WheelFrontLeft");

wheel_fl.center();

box = (BoundingBox) wheel_fl.getModelBound();

player.addWheel(wheel_fl.getParent(), box.getCenter().add(0, -front_wheel_h, 0),

wheelDirection, wheelAxle, 0.2f, wheelRadius, true);



Geometry wheel_br = findGeom(carNode, "WheelBackRight");

wheel_br.center();



box = (BoundingBox) wheel_br.getModelBound();

player.addWheel(wheel_br.getParent(), box.getCenter().add(0, -back_wheel_h, 0),

wheelDirection, wheelAxle, 0.2f, wheelRadius, false);



Geometry wheel_bl = findGeom(carNode, "WheelBackLeft");

wheel_bl.center();



box = (BoundingBox) wheel_bl.getModelBound();

player.addWheel(wheel_bl.getParent(), box.getCenter().add(0, -back_wheel_h, 0),

wheelDirection, wheelAxle, 0.2f, wheelRadius, false);



player.getWheel(2).setFrictionSlip(4);

player.getWheel(3).setFrictionSlip(4);



//To make rotation and move work add the Vehicle control after adding all the

//wheels

//Move the addControl player to here. It seems that once all the wheels are attached to the VehicleControl node then the whole //vehicle behaves as one

carNode.rotate(0, -FastMath.HALF_PI, 0);

carNode.move(-3,-5,0);

carNode.addControl(player);



rootNode.attachChild(carNode);



getPhysicsSpace().add(player);





}[/java]

No, what the manual says is that physics works all in world space. Also why you take the parent to check for the rotation? The VehicleControl rotates exactly the spatial you pass, no matter if its a geometry or node.

I was checking the parent to rotate all the wheels with the parent node. Like it is explained in scene graph for dummies. again I thought I was dealing with a node and with the physics.



Now that you mention:


No, what the manual says is that physics works all in world space. Also why you take the parent to check for the rotation? The VehicleControl rotates exactly the spatial you pass, no matter if its a geometry or node.


Maybe that is why when I was doing the rotation to physics the whole simulation seemed to go crazy.