Physics tutorial - making FPS game

Hey,



I tried out the Hello Physics tutorial where you shoot cannonballs at the brick wall.

Now I tried to make a test program of my own.



My goal is to put the camera at the position of my PhysicsCharacterNode and to be able to shoot cannonballs.



I tried this but I failed to get the correct behavior.



When I use cam without modifying it, it works fine, I set some velocity cannonballNode.setLinearVelocity(cam.getDirection().mult(500f)); to the cannonball and I am alright.

But now, as soon as I put cam.setLocation(player.getLocalTranslation()); in simpleUpdate method (where player is a PhysicsCharacterNode) the behavior changes.



I start the game, when I shoot in front of me the ball flies at the direction of my crosshair, when I turn around and shoot, cannon ball still comes out, but behind me and I get moved forward a little (that may be mass, but it doesn’t happen shooting in the other side)!

So the more I turn the more wrong it goes.





[java]

private PhysicsCharacterNode player;



simpleInitApp

player = new PhysicsCharacterNode(new CapsuleCollisionShape(1.5f, 6f, 1), .5f);

player.setJumpSpeed(40);

player.setFallSpeed(80);

player.setGravity(80);

player.setLocalTranslation(new Vector3f(0, 210, 0));

rootNode.attachChild(player);

bulletAppState.getPhysicsSpace().add(player);



simpleUpdate

cam.setLocation(player.getLocalTranslation());



onAction shoot

PhysicsNode cannonballNode = new PhysicsNode(

fire, // geometry

spearCollisionShape, // collision shape

5.0f);

cannonballNode.setLocalTranslation(cam.getLocation());

rootNode.attachChild(cannonballNode);

bulletAppState.getPhysicsSpace().add(cannonballNode);

cannonballNode.setLinearVelocity(cam.getDirection().mult(500f));

[/java]



As I said, without cam.setLocation(player.getLocalTranslation()); I get the correct behavior but I use the regular flyby mode. But as soon as I add this line it changes.







Does anyone know whats up?

Hello!



If I had to wager a guess, I’d say it could have something to do with the fact that the camera is now inside the player character node. This in itself is no problem and likely what you want. The issue I’m seeing is that the cannonballs are spawned from the camera’s location. When you go to fire a cannonball, the cannonball is starting inside the playerCharacterNode’s collision shape. I’m somewhat surprised that even weirder things don’t happen when you fire, but in any case; you probably want to put the cannonball firing position outside the shape a little, or away from the camera a little. I’d try something like:



[java]

cannonballNode.setLocalTranslation(new Vector3f(cam.getLocation().x, cam.getLocation().y, cam.getLocation.z + “some amount”));

[/java]



Another thing, I notice that you’re attaching the cannonballNode to the rootNode? If you want the firing position of the cannonballs to move with the player, then you may consider attaching it to the player instead. ~Though I’m not sure if I’m right on this, it may work as written…



Cheers!

~FlaH

Thanks for so fast reply! :slight_smile:



I tried it out and it really works.

Though when I attach the cannonball (which uses particles so it looks kind of like a flare) to player node and shoot for some reason it appears above me and when I move it gently moves with me like a slow shooting star in the sky.

When it’s attached to the rootNode then it shoots out correctly.

I’m doing something similar to what you want to achieve in my game. Basically attach it to the rootNode and then set its position by getting information about where the camera is and what direction it’s facing it. The following code gets the camera’s location and direction and places the cannon ball a set distance from the camera’s location in the direction the camera is facing. (if you set the velocity of the cannonball to be in the direction of the camera as well it’ll act as an aimed projectile)



[java]

/** position the cannon ball*/

cannonballNode.setLocalTranslation(cam.getLocation().add(cam.getDirection().mult(2)));

[/java]

Theres collision groups you know? :wink:

So I add my box to a collisiongroup different than group 1, and tell my cannonball to collide with that group?

Or rather I should put objects I don’t want it to collide with to other group?



And all in 1 collision group collide with eachother by default?



:slight_smile:


normen said:
Theres collision groups you know? ;)

Its all documented in the javadoc, just look at the methods and read it. Its a bit more tricky than you say but also more powerful. Each node has a collideGroup mask and a collideWithGroup mask. This means a node can have multiple collideGroups and collideWithGroups. When one node of a collision pair has one of the groups of the other node in its collideWithGroups the nodes will collide.