I’m having trouble colliding multiple Geometries; whenever I do
geometry.collideWith(otherGeometry, results);
An UnsupportedCollisionException is thrown.
SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
com.jme3.collision.UnsupportedCollisionException
The javadoc on Collidable and Geometry says that collideWith() should be usable with any Collidable objects. If I try to collide a, say, ray
ray.collideWith(otherGeometry, results);
everything works properly. I can post code if it would help you guys, but I feel like there is something wrong with what I am doing on the surface level.
Thanks!
I suppose it might be relevant, Geometry is being moved along with the cam (which is in first person perspective in a world with some sort of physics), and otherGeometry is not being moved on the ground.The idea is that Geometry is a ‘hitbox’ for the player, and the player can walk over ‘traps’ on the ground represented by otherGeometry. It is not necessary for the Geometries to necessarily bump into each other, but that the player walking over the otherGeometry. I intended to use collideWith() to detect if the player’s ‘hitbox’ had intersected the Geometry ‘trap’ on the floor.
However, switching the order of collideWith()
(e.g., otherGeometry.collideWith(Geometry, results); )
Yields the same exception.
I also have tried setting each Geometry onto a node, and having the node move around with the player, as well as colliding the 2 nodes… Still throws same exception.
(messy, poorly organized code ahead)
Relevant code at line 180 and onwards… I make the two Geometries at lines 70-80
[java]
import com.jme3.app.SimpleApplication;
import com.jme3.asset.plugins.ZipLocator;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.*;
import com.jme3.bullet.collision.shapes.CompoundCollisionShape;
import com.jme3.collision.CollisionResults;
import com.jme3.font.BitmapText;
import com.jme3.material.Material;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Ray;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Cylinder;
import jme3tools.optimize.GeometryBatchFactory;
public class Crawl3rMain extends SimpleApplication implements ActionListener {
private BulletAppState bulletAppState;
private CharacterControl player;
private Vector3f walkDirection = new Vector3f();
private boolean left = false, right = false, up = false, down = false;
private MapManager mapmanager;
private Map map;
private Node trueroot, level1, level10, levelplayer;
private Trap trap;
private Geometry collidablePlayer, playermodel, testplayer;
public void simpleInitApp() {
//Init map
level1 = new Node(“level 1”);
levelplayer = new Node(“Represents the player as a node”);
trueroot = new Node(“TRUE ROOT”);
rootNode.attachChild(trueroot);
GeometryBatchFactory f = new GeometryBatchFactory();
trueroot.attachChild(level1);
initMap();
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
//Init player
CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(1.5f, 6f, 1);
player = new CharacterControl(capsuleShape, 0.05f);
player.setJumpSpeed(20);
player.setFallSpeed(30);
player.setGravity(30);
player.setPhysicsLocation(new Vector3f(0, 300, 0));
bulletAppState.getPhysicsSpace().add(player);
Material trapmat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
trapmat.setColor(“Color”, ColorRGBA.Red);
trap = new Trap(trapmat);
//Create a new Box with similar dimensions to the player, which will follow player’s camera around…
//For collision.
Box playercyl = new Box(new Vector3f(0, 0, 0), 1.5f, 7, 1.5f);
collidablePlayer = new Geometry(“Player’s collision box”, playercyl);
collidablePlayer.setMaterial(trapmat);
collidablePlayer.setLocalRotation(new Quaternion().fromAngles(FastMath.PI / 2, 0, 0));
levelplayer.attachChild(collidablePlayer);
rootNode.attachChild(levelplayer);
//End Init player
//Attempt at collidable thing on the ground…
testplayer = new Geometry(“Testplayer”, playercyl);
testplayer.setMaterial(trapmat);
rootNode.attachChild(testplayer);
//
RigidBodyControl truerootphy = new RigidBodyControl(0.0f);
trueroot.addControl(truerootphy);
bulletAppState.getPhysicsSpace().add(truerootphy);
f.optimize(level1);
//Init keys
setUpKeys();
//initCrossHairs();
}
private void initMap() {
Material floormat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
floormat.setTexture(“ColorMap”, assetManager.loadTexture(“Textures/ColoredTex/Monkey.png”));
Material wallmat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
wallmat.setTexture(“ColorMap”, assetManager.loadTexture(“Textures/Terrain/BrickWall/BrickWall.jpg”));
mapmanager = new MapManager(wallmat, floormat);
map = new Map(mapmanager.parseFile(“map.txt”));
map.attachMapToNode(level1);
}
private void setUpKeys() {
inputManager.addMapping(“Left”, new KeyTrigger(KeyInput.KEY_A));
inputManager.addMapping(“Right”, new KeyTrigger(KeyInput.KEY_D));
inputManager.addMapping(“Up”, new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping(“Down”, new KeyTrigger(KeyInput.KEY_S));
inputManager.addMapping(“Jump”, new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addListener(this, “Left”);
inputManager.addListener(this, “Right”);
inputManager.addListener(this, “Up”);
inputManager.addListener(this, “Down”);
inputManager.addListener(this, “Jump”);
}
/** These are our custom actions triggered by key presses.
- We do not walk yet, we just keep track of the direction the user pressed. */
public void onAction(String binding, boolean value, float tpf) {
if (binding.equals("Left")) {
if (value) {
left = true;
} else {
left = false;
}
} else if (binding.equals("Right")) {
if (value) {
right = true;
} else {
right = false;
}
} else if (binding.equals("Up")) {
if (value) {
up = true;
} else {
up = false;
}
} else if (binding.equals("Down")) {
if (value) {
down = true;
} else {
down = false;
}
} else if (binding.equals("Jump")) {
player.jump();
}
}
/**
- This is the main event loop–walking happens here.
- We check in which direction the player is walking by interpreting
- the camera direction forward (camDir) and to the side (camLeft).
- The setWalkDirection() command is what lets a physics-controlled player walk.
- We also make sure here that the camera moves with player.
*/
@Override
public void simpleUpdate(float tpf) {
Vector3f camDir = cam.getDirection().clone().multLocal(0.6f);
Vector3f camLeft = cam.getLeft().clone().multLocal(0.4f);
walkDirection.set(0, 0, 0);
if (left) {
walkDirection.addLocal(camLeft);
}
if (right) {
walkDirection.addLocal(camLeft.negate());
}
if (up) {
walkDirection.addLocal(camDir);
}
if (down) {
walkDirection.addLocal(camDir.negate());
}
player.setWalkDirection(walkDirection);
collidablePlayer.setLocalTranslation(player.getPhysicsLocation());
cam.setLocation(player.getPhysicsLocation());
//levelplayer.setLocalTranslation(player.getPhysicsLocation());
Ray ray = new Ray(cam.getLocation(), cam.getDirection());
CollisionResults c = new CollisionResults();
collidablePlayer.collideWith(testplayer, c);
if (c.size() > 0) {
System.out.println("Some sort of collision happened…");
}
System.out.println("out!");
}
protected void initCrossHairs() {
guiNode.detachAllChildren();
guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
BitmapText ch = new BitmapText(guiFont, false);
ch.setSize(guiFont.getCharSet().getRenderedSize() * 2);
ch.setText("X"); // crosshairs
ch.setLocalTranslation( // center
settings.getWidth() / 2 - guiFont.getCharSet().getRenderedSize() / 3 * 2,
settings.getHeight() / 2 + ch.getLineHeight() / 2, 0);
guiNode.attachChild(ch);
}
public static void main(String[] args) {
Crawl3rMain c = new Crawl3rMain();
c.start();
}
}[/java]
General geometry to geometry collisions are not supported by the collideWith() method. Mesh collisions are officially “really difficult” and are about 50% of what makes up a typical physics engine.
…thus you have bullet.
Do you have a recommendation about what I would like to do, then? I am not sure how to approach it with bullet in the way which I want to have it done (not getting “stuck” on the ‘trap’ but stepping through it). Is there a way to do it without using bullet, e.g other types of collideWith()?
I don’t. I’m officially a crazy person because I wrote my own physics engine. (Seriously, life will never be the same again.)
One of the other JME bullet users is bound to chime in though.
Why don’t you want to use bullet for collision? If you set the objects to be kinematic you dont get anyphysics, just collision. And the raytest of bullet is much faster than collideWith()
Mainly because I was under the impression that there is a better way.
I am not sure how to approach this with bullet, I do not know how to test for collisions with bullet.
I imagine that it would be somewhat like the procedure described here: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:physics
For each Spatial that you want to be physical:
Create a CollisionShape.
Create a PhysicsControl by supplying the CollisionShape and mass.
E.g. com.jme3.bullet.control.RigidBodyControl
Add the PhysicsControl to the Spatial.
Add the PhysicsControl to the physicsSpace object.
Attach the Spatial to the rootNode, as usual.
(Optional) Implement the PhysicsCollisionListener interface to respond to PhysicsCollisionEvents if desired.
Is there a code sample for something like I am trying to do? I cannot figure out how to do it, here is some code that I have been working on…
import com.jme3.app.SimpleApplication;
import com.jme3.asset.plugins.ZipLocator;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.PhysicsCollisionEvent;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.*;
import com.jme3.bullet.collision.PhysicsCollisionListener;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.collision.shapes.CompoundCollisionShape;
import com.jme3.collision.CollisionResults;
import com.jme3.font.BitmapText;
import com.jme3.material.Material;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Ray;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Cylinder;
import jme3tools.optimize.GeometryBatchFactory;
[java]public class Crawl3rMain extends SimpleApplication implements ActionListener, PhysicsCollisionListener {
private BulletAppState bulletAppState;
private CharacterControl player;
private Vector3f walkDirection = new Vector3f();
private boolean left = false, right = false, up = false, down = false;
private MapManager mapmanager;
private Map map;
private Node trueroot, level1, level10, levelplayer;
private Trap trap;
private Geometry collidablePlayer, playermodel, testplayer;
public void simpleInitApp() {
//Init map
level1 = new Node(“level 1”);
levelplayer = new Node(“Represents the player as a node”);
trueroot = new Node(“TRUE ROOT”);
rootNode.attachChild(trueroot);
GeometryBatchFactory f = new GeometryBatchFactory();
trueroot.attachChild(level1);
initMap();
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
//Init player
CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(1.5f, 6f, 1);
player = new CharacterControl(capsuleShape, 0.05f);
player.setJumpSpeed(20);
player.setFallSpeed(30);
player.setGravity(30);
player.setPhysicsLocation(new Vector3f(0, 300, 0));
bulletAppState.getPhysicsSpace().add(player);
Material trapmat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
trapmat.setColor(“Color”, ColorRGBA.Red);
trap = new Trap(trapmat);
//Create a new Box with similar dimensions to the player, which will follow player’s camera around…
//For collision.
Box playercyl = new Box(new Vector3f(0, 0, 0), 1.5f, 7, 1.5f);
collidablePlayer = new Geometry(“Player’s collision box”, playercyl);
collidablePlayer.setMaterial(trapmat);
RigidBodyControl collidablePlayerRBC = new RigidBodyControl(0f);
collidablePlayer.addControl(collidablePlayerRBC);
collidablePlayerRBC.setKinematic(true);
bulletAppState.getPhysicsSpace().add(collidablePlayer);
rootNode.attachChild(collidablePlayer);
collidablePlayer.setLocalRotation(new Quaternion().fromAngles(FastMath.PI / 2, 0, 0));
playermodel = new Geometry(“Player2’s collison box”, playercyl);
playermodel.setMaterial(trapmat);
RigidBodyControl playermodelRBC = new RigidBodyControl();
playermodel.addControl(playermodelRBC);
playermodelRBC.setKinematic(true);
bulletAppState.getPhysicsSpace().add(playermodel);
rootNode.attachChild(playermodel);
//End Init player
//Attempt at collidable thing on the ground…
//
//MAGICAL CODE AHEAD: SAVES TIME, NO NEED TO RENDER EVERYTHING INDIVIDUALLY?
RigidBodyControl truerootphy = new RigidBodyControl(0.0f);
trueroot.addControl(truerootphy);
bulletAppState.getPhysicsSpace().add(truerootphy);
f.optimize(level1);
//Init keys
setUpKeys();
//initCrossHairs();
bulletAppState.getPhysicsSpace().enableDebug(assetManager);
}
private void initMap() {
Material floormat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
floormat.setTexture(“ColorMap”, assetManager.loadTexture(“Textures/ColoredTex/Monkey.png”));
Material wallmat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
wallmat.setTexture(“ColorMap”, assetManager.loadTexture(“Textures/Terrain/BrickWall/BrickWall.jpg”));
mapmanager = new MapManager(wallmat, floormat);
map = new Map(mapmanager.parseFile(“map.txt”));
map.attachMapToNode(level1);
}
private void setUpKeys() {
inputManager.addMapping(“Left”, new KeyTrigger(KeyInput.KEY_A));
inputManager.addMapping(“Right”, new KeyTrigger(KeyInput.KEY_D));
inputManager.addMapping(“Up”, new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping(“Down”, new KeyTrigger(KeyInput.KEY_S));
inputManager.addMapping(“Jump”, new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addListener(this, “Left”);
inputManager.addListener(this, “Right”);
inputManager.addListener(this, “Up”);
inputManager.addListener(this, “Down”);
inputManager.addListener(this, “Jump”);
}
/** These are our custom actions triggered by key presses.
- We do not walk yet, we just keep track of the direction the user pressed. */
public void onAction(String binding, boolean value, float tpf) {
if (binding.equals("Left")) {
if (value) {
left = true;
} else {
left = false;
}
} else if (binding.equals("Right")) {
if (value) {
right = true;
} else {
right = false;
}
} else if (binding.equals("Up")) {
if (value) {
up = true;
} else {
up = false;
}
} else if (binding.equals("Down")) {
if (value) {
down = true;
} else {
down = false;
}
} else if (binding.equals("Jump")) {
player.jump();
}
}
/**
- This is the main event loop–walking happens here.
- We check in which direction the player is walking by interpreting
- the camera direction forward (camDir) and to the side (camLeft).
- The setWalkDirection() command is what lets a physics-controlled player walk.
- We also make sure here that the camera moves with player.
*/
@Override
public void simpleUpdate(float tpf) {
Vector3f camDir = cam.getDirection().clone().multLocal(0.6f);
Vector3f camLeft = cam.getLeft().clone().multLocal(0.4f);
walkDirection.set(0, 0, 0);
if (left) {
walkDirection.addLocal(camLeft);
}
if (right) {
walkDirection.addLocal(camLeft.negate());
}
if (up) {
walkDirection.addLocal(camDir);
}
if (down) {
walkDirection.addLocal(camDir.negate());
}
player.setWalkDirection(walkDirection);
//playermodel.setLocalTranslation(player.getPhysicsLocation());
cam.setLocation(player.getPhysicsLocation());
//levelplayer.setLocalTranslation(player.getPhysicsLocation());
Ray ray = new Ray(cam.getLocation(), cam.getDirection());
CollisionResults c = new CollisionResults();
//collidablePlayer.collideWith(testplayer, c);
if (c.size() > 0) {
//System.out.println("Some sort of collision happened…");
}
//System.out.println("out!");
}
protected void initCrossHairs() {
guiNode.detachAllChildren();
guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
BitmapText ch = new BitmapText(guiFont, false);
ch.setSize(guiFont.getCharSet().getRenderedSize() * 2);
ch.setText("X"); // crosshairs
ch.setLocalTranslation( // center
settings.getWidth() / 2 - guiFont.getCharSet().getRenderedSize() / 3 * 2,
settings.getHeight() / 2 + ch.getLineHeight() / 2, 0);
guiNode.attachChild(ch);
}
public static void main(String[] args) {
Crawl3rMain c = new Crawl3rMain();
c.start();
}
public void collision(PhysicsCollisionEvent event) {
System.out.println("COLLISION!");
}
}[/java]
try physicsSpace.rayTest
I’ve been playing around with the physicsSpace.rayTest(), but I cannot parse the PhysicsRayTestResults for any sort of defining characteristics about my collidablePlayer; the most I can ‘use’ the results for at the moment is seeing if my player is on the same x and z coordinates as my Player.
EDIT: I have thought about something… should I keep my collidablePlayer on a new BulletAppState, and check for collisions with its physicsState ? Or is there a way to do what I am looking to do with one BulletAppState?
On line 210, you can see me trying to work through the PhysicsRayTestResults array.
[java]
import com.jme3.app.SimpleApplication;
import com.jme3.asset.plugins.ZipLocator;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.PhysicsCollisionEvent;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.*;
import com.jme3.bullet.collision.PhysicsCollisionListener;
import com.jme3.bullet.collision.PhysicsRayTestResult;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.collision.shapes.CompoundCollisionShape;
import com.jme3.collision.CollisionResults;
import com.jme3.font.BitmapText;
import com.jme3.material.Material;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Ray;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Cylinder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import jme3tools.optimize.GeometryBatchFactory;
public class Crawl3rMain extends SimpleApplication implements ActionListener, PhysicsCollisionListener {
private BulletAppState bulletAppState;
private CharacterControl player;
private Vector3f walkDirection = new Vector3f();
private boolean left = false, right = false, up = false, down = false;
private MapManager mapmanager;
private Map map;
private Node trueroot, level1, level10, levelplayer, n;
private Trap trap;
private Geometry collidablePlayer, playermodel, testplayer;
public void simpleInitApp() {
//Init map
level1 = new Node(“level 1”);
levelplayer = new Node(“Represents the player as a node”);
trueroot = new Node(“TRUE ROOT”);
rootNode.attachChild(trueroot);
GeometryBatchFactory f = new GeometryBatchFactory();
trueroot.attachChild(level1);
initMap();
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
//Init player
CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(1.5f, 6f, 1);
player = new CharacterControl(capsuleShape, 0.05f);
player.setJumpSpeed(20);
player.setFallSpeed(30);
player.setGravity(30);
player.setPhysicsLocation(new Vector3f(0, 300, 0));
n = new Node(“Trap node?”);
bulletAppState.getPhysicsSpace().add(player);
Material trapmat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
trapmat.setColor(“Color”, ColorRGBA.Red);
trap = new Trap(trapmat);
//Create a new Box with similar dimensions to the player, which will follow player’s camera around…
//For collision.
Box playercyl = new Box(new Vector3f(0, 0, 0), 1.5f, 7, 1.5f);
collidablePlayer = new Geometry(“Player’s collision box”, playercyl);
collidablePlayer.setMaterial(trapmat);
RigidBodyControl collidablePlayerRBC = new RigidBodyControl(0f);
collidablePlayer.addControl(collidablePlayerRBC);
collidablePlayerRBC.setKinematic(true);
bulletAppState.getPhysicsSpace().add(collidablePlayer);
n.attachChild(collidablePlayer);
rootNode.attachChild(n);
collidablePlayer.setLocalRotation(new Quaternion().fromAngles(FastMath.PI / 2, 0, 0));
playermodel = new Geometry(“Player2’s collison box”, playercyl);
playermodel.setMaterial(trapmat);
RigidBodyControl playermodelRBC = new RigidBodyControl();
playermodel.addControl(playermodelRBC);
//playermodelRBC.setKinematic(true);
//bulletAppState.getPhysicsSpace().add(playermodel);
//rootNode.attachChild(playermodel);
//End Init player
//Attempt at collidable thing on the ground…
//
//MAGICAL CODE AHEAD: SAVES TIME, NO NEED TO RENDER EVERYTHING INDIVIDUALLY?
RigidBodyControl truerootphy = new RigidBodyControl(0.0f);
trueroot.addControl(truerootphy);
bulletAppState.getPhysicsSpace().add(truerootphy);
f.optimize(level1);
//Init keys
setUpKeys();
//initCrossHairs();
bulletAppState.getPhysicsSpace().enableDebug(assetManager);
}
private void initMap() {
Material floormat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
floormat.setTexture(“ColorMap”, assetManager.loadTexture(“Textures/ColoredTex/Monkey.png”));
Material wallmat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
wallmat.setTexture(“ColorMap”, assetManager.loadTexture(“Textures/Terrain/BrickWall/BrickWall.jpg”));
mapmanager = new MapManager(wallmat, floormat);
map = new Map(mapmanager.parseFile(“map.txt”));
map.attachMapToNode(level1);
}
private void setUpKeys() {
inputManager.addMapping(“Left”, new KeyTrigger(KeyInput.KEY_A));
inputManager.addMapping(“Right”, new KeyTrigger(KeyInput.KEY_D));
inputManager.addMapping(“Up”, new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping(“Down”, new KeyTrigger(KeyInput.KEY_S));
inputManager.addMapping(“Jump”, new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addListener(this, “Left”);
inputManager.addListener(this, “Right”);
inputManager.addListener(this, “Up”);
inputManager.addListener(this, “Down”);
inputManager.addListener(this, “Jump”);
}
/** These are our custom actions triggered by key presses.
- We do not walk yet, we just keep track of the direction the user pressed. */
public void onAction(String binding, boolean value, float tpf) {
if (binding.equals("Left")) {
if (value) {
left = true;
} else {
left = false;
}
} else if (binding.equals("Right")) {
if (value) {
right = true;
} else {
right = false;
}
} else if (binding.equals("Up")) {
if (value) {
up = true;
} else {
up = false;
}
} else if (binding.equals("Down")) {
if (value) {
down = true;
} else {
down = false;
}
} else if (binding.equals("Jump")) {
player.jump();
}
}
/**
- This is the main event loop–walking happens here.
- We check in which direction the player is walking by interpreting
- the camera direction forward (camDir) and to the side (camLeft).
- The setWalkDirection() command is what lets a physics-controlled player walk.
- We also make sure here that the camera moves with player.
*/
@Override
public void simpleUpdate(float tpf) {
Vector3f camDir = cam.getDirection().clone().multLocal(0.6f);
Vector3f camLeft = cam.getLeft().clone().multLocal(0.4f);
walkDirection.set(0, 0, 0);
if (left) {
walkDirection.addLocal(camLeft);
}
if (right) {
walkDirection.addLocal(camLeft.negate());
}
if (up) {
walkDirection.addLocal(camDir);
}
if (down) {
walkDirection.addLocal(camDir.negate());
}
player.setWalkDirection(walkDirection);
//playermodel.setLocalTranslation(player.getPhysicsLocation());
cam.setLocation(player.getPhysicsLocation());
//levelplayer.setLocalTranslation(player.getPhysicsLocation());
Ray ray = new Ray(cam.getLocation(), cam.getDirection());
//System.out.println("out!");
PhysicsRayTestResult rt = new PhysicsRayTestResult();
ArrayList<PhysicsRayTestResult> rtl = new ArrayList<PhysicsRayTestResult>();
bulletAppState.getPhysicsSpace().rayTest(cam.getLocation(), new Vector3f(cam.getLocation().getX(),n.getLocalTranslation().getY(),cam.getLocation().getZ()), rtl);
if (rtl.size() > 0) {
System.out.println("collision detected");
for(PhysicsRayTestResult r : rtl){
if(collidablePlayer.getName().equals(r.toString()))
{
System.out.println("good collision with collidablePlayer");
}
}
}
System.out.println("out!");
}
protected void initCrossHairs() {
guiNode.detachAllChildren();
guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
BitmapText ch = new BitmapText(guiFont, false);
ch.setSize(guiFont.getCharSet().getRenderedSize() * 2);
ch.setText("X"); // crosshairs
ch.setLocalTranslation( // center
settings.getWidth() / 2 - guiFont.getCharSet().getRenderedSize() / 3 * 2,
settings.getHeight() / 2 + ch.getLineHeight() / 2, 0);
guiNode.attachChild(ch);
}
public static void main(String[] args) {
Crawl3rMain c = new Crawl3rMain();
c.start();
}
public void collision(PhysicsCollisionEvent event) {
System.out.println("COLLISION!");
}
}[/java]