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? :?

hallo?

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.

Hello,

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.

[java]

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){

super(cam);

setControlDir(ControlDirection.SpatialToCamera);

this.target = target;

this.inputManager = inputManager;

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

registerInput();

setDefault();

}



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

if (name.equals(“RotateLeft”))

rotateCamera(value);

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

rotateCamera(-value);

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

vRotateCamera(value);

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

vRotateCamera(-value);

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

zoomCamera(-value);

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

zoomCamera(value);

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

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)

return;

rotation += value * rotationSpeed;

if (rotation > FastMath.TWO_PI)

rotation -= FastMath.TWO_PI;

update();

}



private void zoomCamera(float value){

if (!enabled)

return;

distance += value * zoomSpeed * 2;

if (distance > maxDistance)

distance = maxDistance;

if (distance < minDistance)

distance = minDistance;

if ((vRotation < minHeight))

vRotation = minHeight;

update();

}



private void vRotateCamera(float value){

if (!enabled)

return;

vRotation += value * rotationSpeed;



if (vRotation > maxHeight)

vRotation = maxHeight;

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

vRotation = minHeight;

update();

}



public void setDefault(){

rotation = FastMath.PI / 2;

vRotation = 0;

distance = maxDistance;

}



public void update(){

if (target == null)

return;

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);

}

}

[/java]



[java]



public void simpleUpdate(float tpf){

domalaq.update();

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

}

[/java]

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]