JME3 Alpha4 : Moving a Physics Control (Solved)

Hi,



Maybe there already have been post about that.

But since I downloaded the Alpha 4, I,m totally lost :frowning:



I was on a late version of the JME ( from December ) and I used PhysicsNode to manage my objects.



When I wanted to add an object I did something like that :

[java]

Spatial spatial = loadMySpatial();

CapsuleCollisionShape shape = new CapsuleCollisionShape( radius, height );

CharacterPhysicsNode physics = new CharacterPhysicsNode( spatial, shape );

physics.setLocalTranslation( position );



rootNode.attachChild( physics );

physicsSpace.add( physics );

[/java]



Now, I use PhysicsControls. So I tried something like that :

[java]

Spatial spatial = loadMySpatial();

CapsuleCollisionShape shape = new CapsuleCollisionShape( radius, height );

CharacterControl control = new CharacterControl(shape, 0.01f );

spatial.addControl( control );



spatial .setLocalTranslation( position ); ??? (1)

//physics.setPhysicsLocation( position ); ??? ( 2 )



rootNode.attachChild( spatial );

physicsSpace.add( control );

[/java]



But the model is not moved :frowning: ( if I use (1) )

If I use (2) the model seem to be moved but not on the correct direction.



Please, I really need some help here :frowning:



Edit :

Well, after calming myself a bit… I checked my code one more time and I had a problem with the position vector.

It seems that solution (2) is the correct one. Can you just confirn it ? :slight_smile:

You have to set the physics node to kinematic mode if you want to move it continuously with the spatial, otherwise the physics object moves the spatial. Use the control to move the spatial like this:

[java]spatial.getControl(CharacterControl.class).setPhysicsLocation(location);[/java]

As a sidenote, when you attach the control the physics object is moved to the location of the spatial, also when you enable/disable it. So you could simply set the location first and then add the control in your example.

Ok, thanks for the answer. I will use this method in the future :slight_smile:

didialchichi said:
Ok, thanks for the answer. I will use this method in the future :)

Note that you should only move a physics node this way for initial positioning, it *has to be* in kinematic mode otherwise or be moved with forces if you expect it to work properly when colliding with other objects.

Ha…

So if I want to ‘teleport’ one of my character, I have to :


  • switch to kinematic mode
  • use setPhysicsLocation
  • swtich back to classic mode ?



    And one other thing.

    I just saw that I have a problem with the facing part too.

    I used that :

    [java]

    public void faceDirection( float x, float z )

    {

    if( x == 0 && z == 0 )

    z = 1;



    Quaternion modelRotation = new Quaternion();

    Vector3f look = new Vector3f( x * 100000,

    0 ,

    z * 100000 ); //In order to look to the horizon

    modelRotation.lookAt( look, Vector3f.UNIT_Y );

    spatial.setLocalRotation( modelRotation );

    }

    [/java]



    But it seems it doesn’t work anymore. I have to rotate the control instead of the model ?

    Edit : All became easier !!!

    [java]

    physicNode.setViewDirection( new Vector3f( x, 0, z ) );

    [/java]

    Thanks Normen, you just up my mood greatly!

Well, for teleporting you wouldnt expect any collisions so it would be ok. Just don’t continuously move it this way or something.