Camera Node Problem

Hello Sir,

I am using your “TestCameraNode.java” example and its working fine. BUT why i am adding physics(teaNode.addControl(phy):wink: to it, its not working. Camera got stuck and not rotating at all. However forward and backword is working. I am not able to get what’s the issue. I want to add physics so that it should detect collision detection. please help me where is the wrong and how to fix it. Thanks

@pankaj8932 said: Hello Sir,

I am using your “TestCameraNode.java” example and its working fine. BUT when i am adding physics(teaNode.addControl(phy):wink: to it, its not working. Camera got stuck and not rotating at all. However forward and backword is working. I am not able to get what’s the issue. I want to add physics so that it should detect collision detection. please help me where is the wrong and how to fix it.

Below are the piece of code for better understanding of my question.Thanks

BulletAppState bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
phy = new CharacterControl(new CapsuleCollisionShape(1.5f, 6f, 1), .1f);
phy.setPhysicsLocation(new Vector3f(0, 10, 0));
teaGeom = (Geometry) assetManager.loadModel(“Models/Teapot/Teapot.obj”);
Material mat = new Material(assetManager, “Common/MatDefs/Misc/ShowNormals.j3md”);
teaGeom.setMaterial(mat);
//create a node to attach the geometry and the camera node
teaNode = new Node(“teaNode”);
teaNode.attachChild(teaGeom);
teaNode.addControl(phy);
rootNode.attachChild(teaNode);
bulletAppState.getPhysicsSpace().add(phy);

You must manouver the physicsControl when you add physics to it - e.g. setPhysicsLocation, setPhysicsRotation - and add a RigidBodyControl, not a PhysicsControl.

Thanks for ur replay jafella. If you will elaborate little more it will be really helpfull for me. I am using CharacterControl instead of RigidBodyControl. I also tried to add setPhysicsRotation but getting error. Here below is whole code. First execute below code and then Just uncomment this line “teaNode.addControl(phy);” and execute it again you will come know what’s the problem, Camera is getting stuck . It would be really great if u can fix the issue.

package test;

import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.*;
import com.jme3.material.Material;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.scene.CameraNode;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.control.CameraControl.ControlDirection;
import com.jme3.scene.shape.Quad;
import com.jme3.system.AppSettings;

/**

  • A 3rd-person camera node follows a target (teapot). Follow the teapot with
  • WASD keys, rotate by dragging the mouse.
    */
    public class TestCameraNode extends SimpleApplication implements AnalogListener {

private Geometry teaGeom;
private Node teaNode;
CameraNode camNode;
protected CharacterControl phy;
boolean rotate = false;
Vector3f direction = new Vector3f();

public static void main(String[] args) {
TestCameraNode app = new TestCameraNode();
AppSettings s = new AppSettings(true);
s.setFrameRate(100);
app.setSettings(s);
app.start();
}

public void simpleInitApp() {
// load a teapot model
BulletAppState bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
phy = new CharacterControl(new CapsuleCollisionShape(0.5f, 1f, 1), .01f);
phy.setPhysicsLocation(new Vector3f(0, 10, 0));
phy.setGravity(0);
teaGeom = (Geometry) assetManager.loadModel(“Models/Teapot/Teapot.obj”);
Material mat = new Material(assetManager, “Common/MatDefs/Misc/ShowNormals.j3md”);
teaGeom.setMaterial(mat);
//create a node to attach the geometry and the camera node
teaNode = new Node(“teaNode”);
teaNode.attachChild(teaGeom);
//teaNode.addControl(phy);
rootNode.attachChild(teaNode);
bulletAppState.getPhysicsSpace().add(phy);
// create a floor
mat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
mat.setTexture(“ColorMap”, assetManager.loadTexture(“Interface/Logo/Monkey.jpg”));
Geometry ground = new Geometry(“ground”, new Quad(50, 50));
ground.setLocalRotation(new Quaternion().fromAngleAxis(-FastMath.HALF_PI, Vector3f.UNIT_X));
ground.setLocalTranslation(-25, -1, 25);
ground.setMaterial(mat);
rootNode.attachChild(ground);

//creating the camera Node
camNode = new CameraNode("CamNode", cam);
//Setting the direction to Spatial to camera, this means the camera will copy the movements of the Node
camNode.setControlDir(ControlDirection.SpatialToCamera);
//attaching the camNode to the teaNode
teaNode.attachChild(camNode);
//setting the local translation of the cam node to move it away from the teanNode a bit
camNode.setLocalTranslation(new Vector3f(-10, 0, 0));
//setting the camNode to look at the teaNode
camNode.lookAt(teaNode.getLocalTranslation(), Vector3f.UNIT_Y);

//disable the default 1st-person flyCam (don't forget this!!)
flyCam.setEnabled(false);

registerInput();

}

public void registerInput() {
inputManager.addMapping(“rotateRight”, new MouseAxisTrigger(MouseInput.AXIS_X, true));
inputManager.addMapping(“rotateLeft”, new MouseAxisTrigger(MouseInput.AXIS_X, false));
inputManager.addListener(this, “rotateRight”, “rotateLeft”);
}

public void onAnalog(String name, float value, float tpf) {
//computing the normalized direction of the cam to move the teaNode
if (name.equals(“rotateRight”)) {
teaNode.rotate(0, 5 * tpf, 0);
}
if (name.equals(“rotateLeft”)) {
teaNode.rotate(0, -5 * tpf, 0);
}

}
}

What are you trying to achieve exactly? You dont need to add add a RigidBody to anything. You attach the cameraNode to the character spatial, then set its localtranslation to something like Vector3f(0, -1, 0); so that it is situated where the head should be, and that’s it. If you want a chasecamera take a look at TestChaseCamera.java

Did you execute it, if u would have executed it the u would have known what’s the issue. Anyway, I want to rotate my node(teaNode) when I am moving my mouse and at the same time my node(teaNode) should see in the direction of camera. The above code is working fine without adding physics but after adding physics control its not working. Chase camera is something different from this situation. If you still have confusion just execute the above code by commenting and uncommenting.

Sorry I don’t have time right now to run the testcase, I’m kind of in the middle of something and I have like 12 tabs open already in my IDE. If I recall correctly you .setViewDirection(Vector3f) on the characterControl. In this case you just set the viewDirection to the same as the camera - so camera.getDirection();

In the future:

Great thanks for your reply once again. I think the problem is something different from what u r assuming. Please have a look whenever u will get a time. I think this is a case where u will find yourself little bit challenging. I am stuck here please reply soon… :slight_smile:

Hello all, If any one have a time to look into my problem please reply. I have seen many post but didnt get the exact solution what i am looking for. I stuck here from last one week 24x7, this make me feels to quit from gaming development. Please help me, any reply will be greatly appreciated. :cry:

At the risk of sounding offensive, if you aren’t determined enough to work your way through this very well documented problem, you have a snowmans chance in hell of making an entire game. Its hard. Its mathematically mind boggling. It makes you wonder why the hell you ever had the stupid idea of making a game. But it’s also one of the most rewarding areas of programming you’re likely to find :slight_smile: Keep chipping away at it. You will get there eventually.

Your comment demoralizing me and sounds offending too. If you don’t want to give answer please don’t reply. Instead of giving answer u are demoralizing me. You may be expert but while going through the jmonkey tutorial i found one line “one step at a time make champ”, so I am doing that. I wonder on asking question this way people giving answer in this forums. Can anybody(good person) help me on this problem described above?

You have been trying “24x7 for the past week” to get your character face the same direction as the camera , and me telling you to keep trying, despite giving you the answer already, is demoralizing? Setting the view direction via characterControl.setViewDirection(camera.getDirection()) is how you do it. That is the answer.

I think I have to figure out myself what’s the problem. The fix your are mentioning here I am trying this from very first day. Using “setViewDirection” gives me weird rotation of node(teaNode), seems like it jumps from -180 to +180 degree, doesn’t gives smooth rotation. Anyway many thanks for all your help.