character control

Software: Away3D 4.x

vedran, Newbie
Posted: 20 November 2013 05:19 PM   Total Posts: 8

Hi!
I’m working on a game where you drive the spaceship in free space. I’m still learning how to control objects in 3D. So far I have made a spaceship that you can control/drive with keyboard. I would like to add some physics like acceleration and friction and I got stuck in one point. Right now I use this code to move ship:

if (_key.isDown(68)) _ship.moveBackward(10);
if (_key.isDown(83)) _ship.moveForward(10);
if (_key.isDown(38)) _ship.rotationX -= _vx;
if (_key.isDown(40)) _ship.rotationX += _vx;
if (_key.isDown(37)) _ship.rotationY -= _vy;
if (_key.isDown(39)) _ship.rotationY += _vy;

Since i want to add some physics I would like to move it so looks like this:

if (_key.isDown(68)) _ship.moveBackward += 10;
if (_key.isDown(83)) _ship.moveForward += 10;

So, the move method is problematic.
How can i do that using some other function or method?

I have try to use _ship.z += 10 but this does just z translation not moving back and forward because i also use _ship.rotationX and Y.

Thanks!
Vedran

   

Avatar
SharpEdge, Member
Posted: 21 November 2013 10:12 AM   Total Posts: 94   [ # 1 ]

If you want to simulate the real world and use physics you should have some speeds properties that increments while you keep the key pressed, and then each frame move and rotate the spaceship by the speed.
Furthermore you didn’t consider deltaTime (the time between frames) that’s essential to obtain a framerate indipendent movement.
That way you can handle collision setting speed to 0, acceleration and deceleration.

   

vedran, Newbie
Posted: 21 November 2013 10:37 PM   Total Posts: 8   [ # 2 ]

Hi SharpEdge!

I have the speed and other propreties and the code that I posted is executed on ENTER_FRAME. I know that part. I also know about deltaTime but since I’m still learning 3D I would like to make my demos simple. Thanks for your advice anyway!

My problem is how to move 3d object.

This is what i get when i use move methods:
if (_key.isDown(68)) _ship.moveBackward(10);
if (_key.isDown(83)) _ship.moveForward(10);

http://box.onoxo.net/racer/a/

And this is what i get when i use z method:
if (_key.isDown(68)) _ship.z += _vz;
if (_key.isDown(83)) _ship.z -= _vz;

http://box.onoxo.net/racer/b/


What I want is to get the movement from first example but changing “z” value or something more appropriate. Hope it’s more clear now smile

Thanks!
Vedran

   

vedran, Newbie
Posted: 21 November 2013 10:42 PM   Total Posts: 8   [ # 3 ]

Hi SharpEdge!

I have the speed and other propreties and the code that I posted is executed on ENTER_FRAME. I know that part. I also know about deltaTime but since I’m still learning 3D I would like to make my demos simple. Thanks for your advice anyway!

My problem is how to move 3d object.

This is what i get when i use move methods:
if (_key.isDown(68)) _ship.moveBackward(10);
if (_key.isDown(83)) _ship.moveForward(10);

http://box.onoxo.net/racer/a/
(use cursor keys + S and D for moving forward/backward)


And this is what i get when i use z method:
if (_key.isDown(68)) _ship.z += _vz;
if (_key.isDown(83)) _ship.z -= _vz;

http://box.onoxo.net/racer/b/

Do you notice that in B example the ship (blue box) is moving only on Z axis and in the first example using moveForward method the ship is moving in the camera direction. What I want is to get the movement from first example but changing “z” value or something more appropriate. Hope it’s more clear now smile I’m still learning 3d jargon and maybe my description is not very good.

Thanks!
Vedran

   

vedran, Newbie
Posted: 25 November 2013 11:56 AM   Total Posts: 8   [ # 4 ]

Hi!
Can someone help?

Best,
Vedran

   

Avatar
Mu Duck, Newbie
Posted: 25 November 2013 03:31 PM   Total Posts: 20   [ # 5 ]

well you need to change other coordinates as well, if you want it to move forward or backward anyway. It is much simpler with just those methods. You still can do this manually by trying something like this:
1) you need to calculate vector3D for the correct direction (there are many ways to do this depending on your setup, but I would just simply use the camera’s forwardVector property).
2) you need to normalize it (a_vector3D.normalize() method)
3) scale it by your speed number (a_vector3D.scaleBy(Number)) (here is where you can adjust acceleration if you need it - simply use something like this on each frame, while button is pressed (if speed

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X