Hello!
I have been trying to create a RigidBodyControl to represent a physical body in space. This is used to represent a ship but from a first person view. The issue I am having is that when I place the ship in space, it starts to fall as there is no ground to support it.
The reason for using a “xxxControl” class is for the collision detection and movement. (Previously i used VehicleControl but a previous post on this forum from Normen recommended using RigidBodyControl
I’ve read other posts on this forum and searched google as well as the test code. I couldn’t find anything that does something similar. Basically, I would like to implement the first person shooter example HelloCollision (with keys and movement etc) but in space (similar to the HelloPhysics example).
Any advice / pointers / help would be welcome.
The following is portions of the code I am using:
public class Encounter extends SimpleApplication {
public static void main(String args[]) {
Encounter app = new Encounter();
app.start();
}
private BulletAppState bulletAppState;
private RigidBodyControl player;
private final Vector3f originalPosVector = new Vector3f(0, 4f, 6f);
@Override
public void simpleInitApp() {
/** Set up Physics Game /
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
/* Configure cam to look at scene */
cam.setLocation(originalPosVector);
cam.lookAt(new Vector3f(2, 2, 0), Vector3f.UNIT_Y);
initPlayer();
bulletAppState.getPhysicsSpace().enableDebug(assetManager);
}
protected void initPlayer() {
// fly cam to follow player
flyCam.setMoveSpeed(100);
// create player ship
BoxCollisionShape box = new BoxCollisionShape(new Vector3f(1.2f, 0.5f, 2.4f));
//create ship control
player = new RigidBodyControl(box, 400);
player.setPhysicsLocation(originalPosVector);
//player.setGravity(Vector3f.ZERO);
//player.setFriction(0.0f);
//player.setCollisionGroup(PhysicsCollisionObject.COLLISION_GROUP_02);
//rootNode.addControl(player);
bulletAppState.getPhysicsSpace().add(player);
}
@Override
public void simpleUpdate(float tpf) {
//cam.lookAt(player.getPhysicsLocation(), Vector3f.UNIT_Y);
cam.setLocation(player.getPhysicsLocation());
}
}
Many thanks,
Jatin
I can’t see any ground and/or spatial…
Do you have to create a ground? As its space this should not need to be defined.
If it does, I would need to make it infinite long.
By spatial do you mean content? ( note I removed the other bits of the code, which contained the floor and bricks from the hellocollision code)
Do you have an example?
Cheers
Jatin
I have yet to deal with physics in ANY form or fashion, but does gravity default to ‘ZERO’? From taking a quick look at the HelloPhysics tutorial, gravity is never specified yet the balls fall as if affected by normal gravitational forces.
I noticed that you were manually setting gravity, but you commented the line out. What happens if you uncomment that line?
-Feen
@jt123 said: I would like to implement the first person shooter example HelloCollision (with keys and movement etc) but in space (similar to the HelloPhysics example).
Look at the TestHoverCraft.java example and adapt the cam to first person.
The Gravity is normally earth gravity, you need to set it to Vector3f.Zero
i did a little javadoc research and this is what i came up with…
try moving player.setGravity(Vector3f.ZERO); to a place AFTER you add the control to the physics space.
taken from PhysicsRigidBody javadoc (inherited by RigidBodyControl):
public void setGravity(Vector3f gravity)
Set the local gravity of this PhysicsRigidBody
Set this after adding the node to the PhysicsSpace, the PhysicsSpace assigns its current gravity to the physics node when its added.
Parameters:
gravity - the gravity vector to set
-OR-
if your players will always be in space, you could simply set the gravity of your entire PhysicsSpace to ZERO before you add anything to it, so all spatials will have the same value applied.
taken from PhysicsSpace javadoc:
public void setGravity(Vector3f gravity)
Sets the gravity of the PhysicsSpace, set before adding physics objects!
Parameters:
gravity -
if all you need it for is collision detection, you can just make it kinematic, but it won’t get affected by any physics forces but it can apply forces to other non-kinematic objects
Thanks all!! Decoy’s solution did the trick.