attachChild(camera) doesn't work =(

Hey, I’d like to know if there is a way to attach a camera to an object (so I can have a pursuit camera) because for now when I try to do it, it returns that a camera is not a spatial and can’t be attached to a node.

Any solution ? Thx !

Yes you can use a CameraNode, but make sure that the control dir is SpatialToCamera (camNode.setControlDir(SpatialToCamera ))

Thx, I tried with :



[java]private void initCamera() {

cam.setLocation(new Vector3f(0, 40f, 40f));

cam.lookAt(Vector3f.ZERO, new Vector3f(0, 1, 0));

cam.setFrustumFar(100);

CameraNode cameraNode = new CameraNode("CameraNode", cam);

platform.getPlatformNode().attachChild(cameraNode);

// cameraNode.setControlDir(ControlDirection.SpatialToCamera);

}

[/java]



But I’m having the following error :


SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.NullPointerException
at com.jme3test.hellotest.HelloTest.initCamera(HelloTest.java:98)
at com.jme3test.hellotest.HelloTest.simpleInitApp(HelloTest.java:62)
at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:219)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:117)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:188)
at java.lang.Thread.run(Thread.java:680)


Platform.getPlatformNode is the node I want my cam to attach to.

Well…something is null somewhere, what is at line 98 in your code?

This :

[java]platform.getPlatformNode().attachChild(cameraNode);[/java]

In this case it might be the platform.getPlatformNode… :frowning: I’mma try to check why.

I think the platform node:

[java]platform.getPlatformNode()[/java]

might be null

Well, this problem is solved I actually was using the platform’s node which wasn’t even built yet Oo…



But my cam doesn’t move according to the platform’s node moves :(. I must be missing some point.

uncomment this line “cameraNode.setControlDir(ControlDirection.SpatialToCamera);”

also disable the flyCam

The comment was off actually when i tried. I disabled the flyCam (with flyCam.setEnabled(false)) but still the same.

Could you post the complete code? I can’t see anything else…

Sure, here it is :



[java]package com.jme3test.hellotest;



import com.jme3.app.SimpleApplication;

import com.jme3.bullet.BulletAppState;

import com.jme3.input.KeyInput;

import com.jme3.input.controls.AnalogListener;

import com.jme3.input.controls.KeyTrigger;

import com.jme3.math.Vector3f;

import com.jme3.niftygui.NiftyJmeDisplay;

import com.jme3.renderer.queue.RenderQueue.ShadowMode;

import com.jme3.scene.CameraNode;

import com.jme3.scene.control.CameraControl.ControlDirection;

import com.jme3.shadow.BasicShadowRenderer;

import com.jme3.system.AppSettings;

import com.lesmobilizers.tehmoballizer.items.Ball;

import com.lesmobilizers.tehmoballizer.items.Bonus;

import com.lesmobilizers.tehmoballizer.items.Platform;

import com.lesmobilizers.tehmoballizer.items.Wall;



import de.lessvoid.nifty.Nifty;



public class HelloTest extends SimpleApplication {



private static final float a = 1f;



private Nifty nifty;

private BulletAppState bulletAppState;

private Ball ball;

private Platform platform;



private AnalogListener analogListener = new AnalogListener() {

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

float b = a * tpf;

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

platform.getPlatformNode().rotate(0, 0, -b / 2);

}

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

platform.getPlatformNode().rotate(0, 0, b / 2);

}

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

platform.getPlatformNode().rotate(-b / 2, 0, 0);

}

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

platform.getPlatformNode().rotate(b / 2, 0, 0);

}

}

};



public static void main(String[] args) {

AppSettings settings = new AppSettings(true);

HelloTest test = new HelloTest();

settings.setFullscreen(true);

settings.setResolution(800, 600);

test.setShowSettings(false);

test.start();

}



@Override

public void simpleInitApp() {

bulletAppState = new BulletAppState();

stateManager.attach(bulletAppState);

bulletAppState.getPhysicsSpace().enableDebug(assetManager);

ball = new Ball(new Vector3f(0, 2f, 0), rootNode, assetManager, bulletAppState);

platform = new Platform(new Vector3f(0, 0, 0), 20, 1, 20, rootNode, assetManager, bulletAppState);

Wall wall1 = new Wall(new Vector3f(20, 1.5f, 0), 0.5f, 3, 20, platform.getPlatformNode(), assetManager, bulletAppState);

Bonus bonus1 = new Bonus(new Vector3f(10, 2, 0), platform.getPlatformNode(), assetManager);

initKeys();

initShadows();

initCamera();

// initNiftyGui();

}



private void initShadows() {

BasicShadowRenderer bSR = new BasicShadowRenderer(assetManager, 1024);

bSR.setDirection(new Vector3f(-1, -1, -1).normalizeLocal());

viewPort.addProcessor(bSR);

// bSR.setCompareMode(CompareMode.Software);

rootNode.setShadowMode(ShadowMode.Off);

}



private void initKeys() {

// inputManager.clearMappings();

inputManager.addMapping(“Left”, new KeyTrigger(KeyInput.KEY_J));

inputManager.addMapping(“Right”, new KeyTrigger(KeyInput.KEY_L));

inputManager.addMapping(“Up”, new KeyTrigger(KeyInput.KEY_I));

inputManager.addMapping(“Down”, new KeyTrigger(KeyInput.KEY_K));



// inputManager.addListener(actionListener, new String[] { });

inputManager.addListener(analogListener, new String[] { “Left”,

“Right”, “Up”, “Down” });

}



private void initCamera() {

flyCam.setEnabled(false);

cam.setLocation(new Vector3f(0, 40f, 40f));

cam.lookAt(Vector3f.ZERO, new Vector3f(0, 1, 0));

cam.setFrustumFar(100);

CameraNode cameraNode = new CameraNode(“CameraNode”, cam);

cameraNode.setLocalTranslation(new Vector3f(0, 40f, 40f));

platform.getPlatformNode().attachChild(cameraNode);

cameraNode.setControlDir(ControlDirection.CameraToSpatial);

}



private void initNiftyGui() {

NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager,

inputManager, audioRenderer, guiViewPort);

nifty = niftyDisplay.getNifty();

nifty.fromXml(“assets/Interface/Screens/screensmanager.xml”,

“homescreen”);

// attach the nifty display to the gui view port as a processor

guiViewPort.addProcessor(niftyDisplay);

// disable the fly cam

// flyCam.setEnabled(false);

// flyCam.setDragToRotate(true);

}

}

[/java]



My “items” classes (like the platform one) are separated classes. I put you the platform one so you can have an example of what’s in it. Others a pretty similar :



[java]package com.lesmobilizers.tehmoballizer.items;



import com.jme3.asset.AssetManager;

import com.jme3.bullet.BulletAppState;

import com.jme3.bullet.collision.shapes.BoxCollisionShape;

import com.jme3.bullet.control.RigidBodyControl;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

import com.jme3.math.Vector3f;

import com.jme3.renderer.queue.RenderQueue.ShadowMode;

import com.jme3.scene.Geometry;

import com.jme3.scene.Node;

import com.jme3.scene.shape.Box;



public class Platform extends Box {



private Vector3f position;

private float length;

private float width;

private float height;



private Box platform;

private Geometry platformGeometry;

private Material platformMaterial;

private BoxCollisionShape platformCollisionShape;

private RigidBodyControl platformPhysics;

private Node platformNode;



public Platform(Node rootNode, AssetManager assetManager, BulletAppState bulletAppState) {

this(new Vector3f(0, 0, 0), 1, 1, 1, rootNode, assetManager, bulletAppState);

}



public Platform(Vector3f position, Node rootNode, AssetManager assetManager, BulletAppState bulletAppState) {

this(position, 1, 1, 1, rootNode, assetManager, bulletAppState);

}



public Platform(Vector3f position, float length, float width, float height, Node rootNode, AssetManager assetManager, BulletAppState bulletAppState) {

this.position = position;

this.length = length;

this.width = width;

this.height = height;

this.createPlatform(rootNode, assetManager, bulletAppState);

}



protected void createPlatform(Node rootNode, AssetManager assetManager, BulletAppState bulletAppState) {

platform = new Box(length, width, height);

platformGeometry = new Geometry(“Platform”, platform);

platformMaterial = new Material(assetManager, “Common/MatDefs/Misc/SolidColor.j3md”);

platformCollisionShape = new BoxCollisionShape(new Vector3f(length, width, height));

platformPhysics = new RigidBodyControl(platformCollisionShape, 0f);

platformNode = new Node(“wallNode”);



platformMaterial.setColor(“m_Color”, ColorRGBA.Red);

platformGeometry.setMaterial(platformMaterial);

platformGeometry.setShadowMode(ShadowMode.CastAndReceive);

platformGeometry.setLocalTranslation(position);

platformGeometry.addControl(platformPhysics);

platformPhysics.setKinematic(true);

bulletAppState.getPhysicsSpace().add(platformPhysics);

platformNode.attachChild(platformGeometry);

rootNode.attachChild(platformNode);

}



public Vector3f getPosition() {

return position;

}



public void setPosition(Vector3f position) {

this.position = position;

}



public float getLength() {

return length;

}



public void setLength(float length) {

this.length = length;

}



public float getWidth() {

return width;

}



public Node getPlatformNode() {

return platformNode;

}



public void setWidth(float width) {

this.width = width;

}



public float getHeight() {

return height;

}



public void setHeight(float height) {

this.height = height;

}



}[/java]

I tried to restructure my code in order to see if there would be any changes but I’m still stuck on this problem… Is there anyone knowing why ?



I set my cam up with :



[java] protected void initCamera() {

application.getFlyByCamera().setEnabled(false);

application.getCamera().setLocation(new Vector3f(0, 0.866f2.5f, 0.5f2.5f));

application.getCamera().lookAt(Vector3f.ZERO, new Vector3f(0, 1, 0));

application.getCamera().setFrustumFar(100);

CameraNode cameraNode = new CameraNode(“CameraNode”, application.getCamera());

cameraNode.setLocalTranslation(new Vector3f(0, 2f, 2f));

platform.getPlatformNode().attachChild(cameraNode);

cameraNode.setControlDir(ControlDirection.CameraToSpatial);

}[/java]

May it come from the fact I use rotate on my platformNode instead of setLocalRotation ? I don’t know but maybe the rotate method only applies to the direct spatials of the platformNode or something like that…

Sorry,I didn’t have time to look into your issue.

Does the testCameraMotion test case works for you? (pressing space makes the view rotate around the teapot)

Mmm no I disabled the key settings to create my own ones. Space key is not my trigger key.

No I mean, when you run TestCameraMotion, does pressing the space bar rotate the cam around the teapot…?

Could you link me this test please ? I can’t find it in the documentation…

http://code.google.com/p/jmonkeyengine/source/browse/trunk/engine/src/test/jme3test/animation/TestCameraMotionPath.java

I’ve tested this app and yes it works, when i push space a rotative camera turns on.

Ok Now that i look at it closely you typed :

cameraNode.setControlDir(ControlDirection.CameraToSpatial);



I know we’ve already been through this but did you tested it with

cameraNode.setControlDir(ControlDirection.SpatialToCamera);