Moving an object3D around

Started by coersum, October 19, 2012, 06:25:48 PM

Previous topic - Next topic

coersum

Hi,

I know this has been talked about before and I read quite a bit about in forum but nothing seems to work.
What I have is:

1. I load my serialized objects (ground and quickly made robot, just a meshes with textures) and everything works fine
mesh_plaza = Loader.loadSerializedObject(res.openRawResource(R.raw.plaza4s));
mesh_plaza.setScale(30);
mesh_plaza.setTexture("text_plaza");
mesh_plaza.translate(0, -20, 0);
world.addObject(mesh_plaza);
mesh_plaza.strip();

mesh_robot = Loader.loadSerializedObject(res.openRawResource(R.raw.robots));
mesh_robot.setScale(20);
mesh_robot.setTexture("text_robot");
mesh_robot.translate(0, -35, 0);
mesh_robot.setName("airob");
mesh_robot.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);
world.addObject(mesh_robot);
mesh_robot.strip();


2. In onTouchEvent I set moveSpeed and touchTurn variables (i use a dpad I made for it, so moveSpeed is between -2 and +2, 0 being center of my directional-pad)

3. In the onDrawFrame for turning: (that works GREAT, the robot turns on the it's Y axis)
if (touchTurn != 0) {
//world.getCamera().rotateY(touchTurn);
mesh_robot.rotateY(touchTurn);
}

4. Still in onDrawFrame, for moving I tried quite a few things and found in forum that using the object's getZAxis would give me its current directionto which then I just add to z (or x depending on how it was modeled):
if (moveSpeed != 0) {
SimpleVector robot_turn = mesh_robot.getZAxis();
mesh_robot.translate(robot_turn.x, robot_turn.y, robot_turn.z+moveSpeed);
}

But this gives some very weird results. First, without turning, the robot will go back and forth (his front is initially positioned on the Z axis) but once I start turning all hell breaks loose.
he will often just move sidewise-forward not only in one direction.

Any help or hint would be awesome.
Thank you
PS: next I am going to try to get the camera to follow, get the robot to not go through walls, add gravity so the robot can jump etc...so much to learn.

EgonOlsen

It doesn't work that way. You have to multiply the robot_turn vector with moveSpeed instead of adding it to the z-axis.

coersum

Could you help me on how to do that by chance ?  just can't get anything to work. Been trying to find examples of 3d objects being moved around, with physics etc, I just can't find much sadly.

EgonOlsen

Something like this should actually work:

if (moveSpeed != 0) {
SimpleVector robotTurn = meshRobot.getZAxis();
        robotTurn.scalarMul(moveSpeed);
meshRobot.translate(robotTurn);
}

coersum

thank you so much!! I think I actually tried that but I was having so much trouble going through the doc (not the doc's fault), that I might have had something else added to that which made it not work. Got the Chase-Camera to work finding info on this thread in case someone is looking for it: http://www.jpct.net/forum2/index.php/topic,2588.msg19139.html#msg19139