Camera Collision

I have a room in wich I can move around wich a firstpersonhandler. But if I walk into a wall, I go through it. How I can find out if there is any collision between my cam and the wall? :?


Hey there! Please have a look at this which will teach you how to do such things :slight_smile:

Also, you can use a camnode (which is implemented for jME3 here) and make that physical.

is there a way for jme2?

Yes, two clicks away, also in the wiki.


I’m writing third person shooter and met some problems.

I’ve created a CameraNode object that work fine except for collision.

I want to know is my camera colliding in any moment with walls or other scene elements (this.collideWith(scene, results)), but resuts.size() is always zero.

Could you help me make it work or maybe there is another solution? Thanks.

@tkey: Please post your code.


package battle;

import com.jme3.input.InputManager;

import com.jme3.input.KeyInput;

import com.jme3.input.controls.AnalogListener;

import com.jme3.input.controls.KeyTrigger;

import com.jme3.input.controls.MouseAxisTrigger;

import com.jme3.math.FastMath;

import com.jme3.math.Vector3f;

import com.jme3.renderer.Camera;

import com.jme3.scene.CameraNode;

import com.jme3.scene.Spatial;

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

public class ThirdPersonViewCamera extends CameraNode implements AnalogListener{

private float minHeight = -FastMath.PI / 2 + 0.1f;

private float maxHeight = FastMath.PI / 2 - 0.1f;

private float minDistance = 0.1f;

private float maxDistance = 15.0f;

private float zoomSpeed = 2.0f;

private float rotationSpeed = 2.5f;

private float rotation;

private float vRotation;

private float distance;

private boolean enabled = true;

private Spatial target = null;

private InputManager inputManager;

private Vector3f initialUpVec;

public ThirdPersonViewCamera(Camera cam, Spatial target, InputManager inputManager){


setControlDir(ControlDirection.SpatialToCamera); = target;

this.inputManager = inputManager;

initialUpVec = cam.getUp().clone();




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

if (name.equals(“RotateLeft”))


else if (name.equals(“RotateRight”))


else if (name.equals(“RotateUp”))


else if (name.equals(“RotateDown”))


else if (name.equals(“ZoomIn”))


else if (name.equals(“ZoomOut”))


else if (name.equals(“SetDefault”))



public void registerInput(){

String inputs[] = {“RotateUp”, “RotateDown”, “RotateLeft”, “RotateRight”, “ZoomIn”, “ZoomOut”, “SetDefault”};

inputManager.addMapping(“RotateLeft”, new MouseAxisTrigger(0, false));

inputManager.addMapping(“RotateRight”, new MouseAxisTrigger(0, true));

inputManager.addMapping(“RotateUp”, new MouseAxisTrigger(1, false));

inputManager.addMapping(“RotateDown”, new MouseAxisTrigger(1, true));

inputManager.addMapping(“ZoomIn”, new MouseAxisTrigger(2, false));

inputManager.addMapping(“ZoomOut”, new MouseAxisTrigger(2, true));

inputManager.addMapping(“ZoomIn”, new KeyTrigger(KeyInput.KEY_EQUALS));

inputManager.addMapping(“ZoomOut”, new KeyTrigger(KeyInput.KEY_MINUS));

inputManager.addMapping(“SetDefault”, new KeyTrigger(KeyInput.KEY_BACK));

inputManager.addListener(this, inputs);


private void rotateCamera(float value){

if (!enabled)


rotation += value * rotationSpeed;

if (rotation > FastMath.TWO_PI)

rotation -= FastMath.TWO_PI;



private void zoomCamera(float value){

if (!enabled)


distance += value * zoomSpeed * 2;

if (distance > maxDistance)

distance = maxDistance;

if (distance < minDistance)

distance = minDistance;

if ((vRotation < minHeight))

vRotation = minHeight;



private void vRotateCamera(float value){

if (!enabled)


vRotation += value * rotationSpeed;

if (vRotation > maxHeight)

vRotation = maxHeight;

if ((vRotation < minHeight) && (distance > (minDistance + 1.0f)))

vRotation = minHeight;



public void setDefault(){

rotation = FastMath.PI / 2;

vRotation = 0;

distance = maxDistance;


public void update(){

if (target == null)


float hDistance = distance * FastMath.sin((FastMath.PI / 2) - vRotation);

Vector3f pos = new Vector3f(hDistance * FastMath.cos(rotation), distance * FastMath.sin(vRotation), hDistance * FastMath.sin(rotation));

pos = pos.add(target.getLocalTranslation());

float offset = 1.4f;

if (distance < 1.4f){

offset *= distance / maxDistance;


setLocalTranslation(pos.add(0, offset, 0));

lookAt(target.getLocalTranslation().add(0, offset, 0), initialUpVec);





public void simpleUpdate(float tpf){


personCam.update();//third persom camera

CollisionResults results = new CollisionResults();

personCam.collideWith(arena, results);

if (results.size() > 0)

System.out.println(“collide”); // doesn’t give an output



I presume that ‘arena’ is the ray that you are casting?

If not, then this should work:

[java]Ray ray = new Ray(cam.getLocation(), cam.getDirection());

arena.collideWith(ray, results);[/java]