hi there,
i'm having trouble figuring how to have a valid last position.
so far collision work, but then when i set last position the object stays trapped into the wall.
it seems like i store the last position after colliding , instead of last position before colliding.
thus, everytime i hit a wall, i get stuck in it..
someone could tell me what i'm doing wrong ?
any advices?
thanks !
here's the code:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingBox;
import com.jme.bounding.CollisionTree;
import com.jme.bounding.CollisionTreeManager;
import com.jme.input.InputHandler;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.intersection.CollisionData;
import com.jme.intersection.CollisionResults;
import com.jme.intersection.TriangleCollisionResults;
import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.shape.Box;
import com.jme.util.export.binary.BinaryImporter;
import com.jmex.model.converters.FormatConverter;
import com.jmex.model.converters.ObjToJme;
public class HelloModelLoading extends SimpleGame {
private Node boxNode, mapNode;
private Spatial b, m;
private CollisionResults results;
private CollisionData oldData;
private Vector3f lastPosition;
private float boxSpeed = 100.0f;
public static void main(String[] args) {
HelloModelLoading app = new HelloModelLoading();
app.setConfigShowMode(ConfigShowMode.AlwaysShow);
Logger.getLogger("").setLevel(Level.SEVERE);
app.start();
}
protected void simpleInitGame() {
input = new InputHandler();
CollisionTreeManager.getInstance().setTreeType(CollisionTree.Type.AABB);
results = new TriangleCollisionResults();
URL model=HelloModelLoading.class.getClassLoader().getResource("data/test_01_02.obj");
FormatConverter converter=new ObjToJme();
converter.setProperty("mtllib",model);
ByteArrayOutputStream BO=new ByteArrayOutputStream();
try {
mapNode = new Node("map node");
converter.convert(model.openStream(), BO);
m=(Spatial) BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));
m.setLocalScale(.1f);
m.setModelBound(new BoundingBox());
m.updateModelBound();
boxNode = new Node("box node");
b = new Box("box", new Vector3f(-20,5,0), 1, 1 ,1);
b.setLocalScale(.4f);
b.setModelBound(new BoundingBox());
b.updateModelBound();
rootNode.attachChild(b);
rootNode.attachChild(m);
} catch (Exception e) { // Just in case anything happens
System.out.println("Damn exceptions! O_o n" + e);
e.printStackTrace();
System.exit(0);
}
// change the direction camera is looking at
cam.setAxes(new Vector3f(0,0,-1), new Vector3f(0,1,0), new Vector3f(1,0,0));
KeyBindingManager.getKeyBindingManager().set("moveFwd", KeyInput.KEY_W);
KeyBindingManager.getKeyBindingManager().set("moveBwd", KeyInput.KEY_S);
KeyBindingManager.getKeyBindingManager().set("moveLeft", KeyInput.KEY_A);
KeyBindingManager.getKeyBindingManager().set("moveRight", KeyInput.KEY_D);
}
protected void simpleUpdate() {
//put the cam at the box position
cam.setLocation(new Vector3f(
b.getLocalTranslation().getX(),
b.getLocalTranslation().getY()+1,
b.getLocalTranslation().getZ()));
results.clear();
b.findCollisions(rootNode, results);
if (results.getNumber() > 0){ // if we are inside the dungeon, result = 1
oldData = results.getCollisionData(0);
if (oldData.getTargetTris().size()>0)
{
// if collision with dungeon wall happen set lastPosition for box & cam
b.getLocalTranslation().set(lastPosition);
}
// no collision with dungeon walls, we can move..
else if (oldData.getTargetTris().size()==0)
{
if (KeyBindingManager.getKeyBindingManager().isValidCommand("moveFwd", false))
{
b.getLocalTranslation().y += boxSpeed* timer.getTimePerFrame();
}
if (KeyBindingManager.getKeyBindingManager().isValidCommand("moveBwd", false))
{
b.getLocalTranslation().y -= boxSpeed* timer.getTimePerFrame();
}
if (KeyBindingManager.getKeyBindingManager().isValidCommand("moveLeft", false))
{
b.getLocalTranslation().z -= boxSpeed* timer.getTimePerFrame();
}
if (KeyBindingManager.getKeyBindingManager().isValidCommand("moveRight", false))
{
b.getLocalTranslation().z += boxSpeed* timer.getTimePerFrame();
}
// save last position
lastPosition = b.getLocalTranslation();
}
}
}
}
and screenshot:
