"easy" physics camera... again

Hello people,



I'v been searching and trying all day to find some help on the forums. but i have had no luck.

I have seen that there are loads of question on this subject.

So it would be a good idea to create an tutorial on the specifc subject and put it on the wiki.



The situation i have is as follows.

I create my models in 3dsmax and export them as obj.

In jme i loads there models. add some light, etc



Now i want a camera in this scene that does noting else that the normal cam but has collission/physics. thus stay on the ground and does not go into objects.



first off the code that i have now:


public class HelloWorld extends SimplePhysicsGame{
 
 
    private DynamicPhysicsNode player_node;
    private ChaseCamera chaser;
   private Node lnode = new Node("lightNode");
      
   public static void main(String[] args) {
         
        HelloWorld app = new HelloWorld(); // Create Object
        // Signal to show properties dialog
        app.setConfigShowMode(ConfigShowMode.ShowIfNoConfig);
        app.start(); // Start the program
    }
   
   protected void simpleUpdate() {
      
         KeyBindingManager manager = KeyBindingManager.getKeyBindingManager();
         chaser.update(tpf);
           if (manager.isValidCommand("i")) {
              lnode.getLocalTranslation().y += 0.2;
              rootNode.updateRenderState();
           }
   
           if (manager.isValidCommand("k")) {
              lnode.getLocalTranslation().y += -0.2;
              rootNode.updateRenderState();
           }
   
         
       }
   
   protected void simpleInitGame() {
         ObjToJme converter = new ObjToJme();
         try {
            URL objFile = TestObjJmeWrite.class.getClassLoader().getResource(
                  "models/test01.obj");
            converter.setProperty("mtllib", objFile);
               converter.setProperty("texdir",objFile);
            ByteArrayOutputStream BO = new ByteArrayOutputStream();
            System.out.println("Starting to convert .obj to .jme");
            converter.convert(objFile.openStream(), BO);
               Node model=(Node)BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));
            model.setModelBound(new BoundingBox());
            model.updateModelBound();
            
            StaticPhysicsNode modelContainer = getPhysicsSpace().createStaticNode();
            modelContainer.attachChild(model);
            modelContainer.generatePhysicsGeometry();
            rootNode.attachChild(modelContainer);
   
         } catch (IOException e) {
            System.out.println("error loading model");
            e.printStackTrace();
         }
   
         setupCharacter();
         setupLights();
         
          rootNode.attachChild(lnode);
          KeyBindingManager manager = KeyBindingManager.getKeyBindingManager();
          manager.set("i", KeyInput.KEY_I);
          manager.set("k", KeyInput.KEY_K);
         
         rootNode.updateGeometricState(0.0f, true);
         rootNode.updateRenderState();
         
      }
   
   public void setupCharacter() {
      Box b = new Box("box", new Vector3f(), 2,2,2);
       b.setModelBound(new BoundingBox());
       b.updateModelBound();
      
       player_node = getPhysicsSpace().createDynamicNode();
       player_node.attachChild(b);
       player_node.generatePhysicsGeometry();
       rootNode.attachChild(player_node);
       player_node.updateWorldBound();
      
   
      
   }
    private void setupLights() {
      display.getRenderer().setBackgroundColor(ColorRGBA.lightGray);
   
      lightState.setSeparateSpecular(true);
   
      Sphere LightSphere = new Sphere("lp1", 10, 50, 0.2f);
       LightSphere.setModelBound(new BoundingSphere());
       LightSphere.updateModelBound();
       LightSphere.setLightCombineMode(Spatial.LightCombineMode.Off);
       LightSphere.setDefaultColor(new ColorRGBA(0.0f, 1.0f, 0.0f, 1.0f));
      
   
       SpotLight sp1 = new SpotLight();
       sp1.setDiffuse(new ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f));
       sp1.setAmbient(new ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f));
       sp1.setDirection(new Vector3f(0, -1, 0));
       sp1.setLocation(new Vector3f(0, 10, 0));
       sp1.setAngle(180);
       sp1.setEnabled(true);
   
       SimpleLightNode ln = new SimpleLightNode("spNode", sp1);
       lightState.detachAll();
       lightState.attach(sp1);
     
       lnode.attachChild(ln);
       lnode.attachChild(LightSphere);
   }
}




as you can see i have create a player object. and first i would like to have a chase cam... ??? but have not found a sollution for this.

Second and more important to me is not having a playerobject but only a camera. i'm guessing some sort of invisible object that has collision.. ?? but also i have not a clue how to build this.

i hope some1 is willing to help me out, cause this question is asked allot... :)
when i got it al worked out i shall create a tutorial for this and submit to the wiki's...

Many thanks in advance!

Cheers

Martijn

well first you need to specify exactly what you want.

A ChaseCamera chases a Object (the player usually), so you don't need a object for the camera.


as you can see i have create a player object.

Second and more important to me is not having a playerobject but only a camera.


now what?  :) you just did create a player obejct no ?  :)

heheh bare with me, just learning jme (second week now) :slight_smile:

thanks for the fast reply tho.



i shall explain a bit more. i was looking at the examples found on this forum and everyone is making a player object and then attach a camera to it. the physics is then calculated on this character object (if i am correct…).

So i first tried to create a player object (as a dynamic object cause it's the one that i moving) and then attach a cam to it. but i did not get it to work.



for me, i would like to have a camera that is affected by physics, thus has gravity. and does not go thrue objects (collide with them). but i have no idea where to start and  how to create this.





after thinking even better both option … dunno if that gets to complicated for me. but an option for changing between a chasecam view and a 1person view would rock…

but well first things first…

hope you can help me out!



thanks in advance




maybe try it like that:


  • create a 'player' node
  • create sphere with bounding boxes, attach it to the playerNode
  • attach the player node to a dynamic node, generate physics
  • attach a cameraNode to the physics node (or alternatively a chase camera)
  • now move the physics node around and the camera follows it



    since you move a physics node you will get the usual collision detection.

    you can make the sphere as small or as big as you need to get good collision detection.



    You should use forces (physicNode.addForce()), to move the physics node.

    If you translate the physicsnode directly it can result in jittery movement.

ah you are quick with the replies :slight_smile: good one!



i see what you mean with these thingies:

  • create a 'player' node
  • create sphere with bounding boxes, attach it to the playerNode
  • attach the player node to a dynamic node, generate physics



    above i have already done in the function "createPlayerNode()" only with a box…? but the idea is the same…?


  • attach a cameraNode to the physics node (or alternatively a chase camera)
  • now move the physics node around and the camera follows it



    now this part is more tricky . perhaps you know of / or can show me a small example on how to attach a cam to a object and how to setup a handler to move the node (using forces)… ?
perhaps you know of / or can show me a small example on how to attach a cam to a object

simply attach a cameranode to the player_node.

CameraNode camNode = new CameraNode("camNode",
                DisplaySystem.getDisplaySystem().getRenderer().getCamera());
        player_node.attachChild(camNode);
        // moves the camera relative to the player
        camNode.setLocalTranslation(x, y, z);



and how to setup a handler to move the node (using forces).

well thats really the tricky part :)

a spaceship is really easy to move with forces, but moving a player on the floor can be tricky.
here was one attempt:
http://www.jmonkeyengine.com/jmeforum/index.php?topic=9456.0

you can also take a look at the tests in jmetest.physics.TestFrictionCallback / jmetest.physicstut.Lesson8