Why doesn't the box fall off?

Hi. I’m new to JMonkey Engine and I’m trying to implement a simple game where the player can move a box in a simple platform.



The platform is declared as a RigidBodyControl with 0.0f mass. The box is also a RigidBodyControl with positive mass and I’m using the setPhysicsLocation method to move the box around. The problem is when I move the box outside the platform and it doesn’t fall off.



I tried to set the initial position of the box outside the platform and the box indeed fall off.



Any suggestion?

You seem to do something wrong, post your code. At any instance moving a box with setPhysicsLocation is like warping it around the space, to get proper physics behavior you need to move it with proper physics forces or set it to kinematic mode if you want to move it around while still affecting other objects properly.

Here’s the piece of code I use to move the box (r1 is the RigidBodyControl associated to the box):



[java]private AnalogListener analogListener = new AnalogListener() {

public void onAnalog(String name, float value, float tpf) {

if (name.equals(“Right”)) {

Vector3f v = r1.getPhysicsLocation();

r1.setPhysicsLocation(new Vector3f(v.x+valuespeed5, v.y, v.z));

}

}

};

[/java]



So, if I want to move the box should I apply a force to it? But how can I do that with an analog listener? If I keep pressing the same key the result of the forces will increase and the velocity too.

try [java]bulletAppState.getPhysicsSpace().enableDebug(assetManager);[/java] to see the collision shapes, perhaps something is not right

I tried that but I couldn’t see any problem.

do you set the gravity at any point?

Yes, at the beginning when I declare a new BulletAppState. As I said, if I set the initial position of the box outside the platform, it falls off.



For test purposes if I throw something at the box, when outside the platform, it also falls off.



What’s the best way to move the box? Should I use a RigidBodyControl?

well strictly speaking, you shouldn’t use setPhysicsLocation after its initial position, but

try remove the gravity declaration, don’t ask why :stuck_out_tongue: but its worth a shot

The same happened.

What do you expect that code to do? “new Vector3f(v.x+valuespeed5, v.y, v.z)” doesn’t make much sense to me… It will just “wiggle” the object at a certain location… If the framerate is constant it will just sit there… Also if you keep setting the location of the object when the key is pressed, it obviously cannot fall…

@normen said:
What do you expect that code to do? "new Vector3f(v.x+value*speed*5, v.y, v.z)" doesn't make much sense to me.. It will just "wiggle" the object at a certain location.. If the framerate is constant it will just sit there.. Also if you keep setting the location of the object when the key is pressed, it obviously cannot fall..


I expect that the box moves to the right. And it does.

If I shouldn't use setPhysicsLocation, how can I move the box around?

Right, you get the current location, still the framerate dependence is messed up somehow… You should rethink that code… At any instance it doesn’t fall cause you keep warping it around the space.

So, how can I move the box around? I was using this guide to accomplish that: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:beginner:hello_input_system

I told you in the first reply :roll:

Applying forces?



If i do that using the analog listener, wont my box move extremely fast (since the forces will add up over and over)?

…depends on how you do it… if you don’t want physics why you use it? set the box to kinematic mode and move the spatial instead.

Yes, I can do that, but then the box will not fall off the platform.

you can check when it is off the platform, and do [java]control.setKinematic(false);[/java] i assume that should work

What stops things accelerating for ever in the real world (except in a perfect vacuum of course).



Friction :slight_smile:





I’ve not used the JME3 phsics engine yet but I’d imagine there is some way to either apply friction (which will then cause it to only accelerate until friction matches impulse and then slow down smoothly when you stop pushing) or look at the current speed and reduce your acceleration impulse based on it which has the same effect at the start but means it doesn’t slow down…

1 Like

@zarch is on the right track here, thats basically what I meant by “if you don’t want physics don’t use it”. Just move your spatial down when its off the platform and the player will think its “gravity”.