Collision Detection in SimpleGame

Hey Guys,

Currently struggling with collision detection, I am making a game that extends SimpleGame. I want to prevent my character passing through other objects on the terrain.

I assume that I should be putting code in the simpleUpdate() method? I've been using:

if (player.getWorldBound().intersects(target.getWorldBound())){
   System.out.printline("INTERSECTION DETECTED!!!");

I see the warning on the console, however I am unsure how to move the player back to where they have come from. I was thinking of (using a Vector3f instance variable, lastPosition):

if (player.getWorldBound().intersects(target.getWorldBound())){
   System.out.printline("INTERSECTION DETECTED!!!");

lastPosition = player.getLocalTranslation();

Many Thanks!

Careful with setLocalTranslation();

you only get and set the references, not the values.

Try the following instead:

// backup last pos
Vector3f lastposition = new Vector3f(player.getLocalTranslation());

// set last pos

This way more garbage is generated, but it should work.

hey … nid help … same problem … for instance im using the bike in the flagrush example … i can't stop the bike (set to previous vec) … any ideas?  :expressionless:

this is what you want:

  protected static void adjustEntityPosition(final MoveableEntity p_entity, final BoundingVolume p_worldBound) {
    final Vector3f l_extent = ((BoundingBox) p_worldBound).getExtent(ms_tmp);
    final Vector3f l_center = p_worldBound.getCenter();

    final CurrentBounds l_currentBounds = p_entity.getCurrentBounds();

    final float l_minX = l_currentBounds.getCurrentMinX();
    final float l_reducedExtentX = l_extent.x;
    final float l_objMaxX = l_center.x + l_reducedExtentX;
    final float l_maxX = l_currentBounds.getCurrentMaxX();
    final float l_objMinX = l_center.x - l_reducedExtentX;
    final float l_xDiff = evaluateDifference(l_minX, l_objMaxX, l_maxX, l_objMinX);

    final float l_minZ = l_currentBounds.getCurrentMinZ();
    final float l_reducedExtentZ = l_extent.z;
    final float l_objMaxZ = l_center.z + l_reducedExtentZ;
    final float l_maxZ = l_currentBounds.getCurrentMaxZ();
    final float l_objMinZ = l_center.z - l_reducedExtentZ;
    final float l_zDiff = evaluateDifference(l_minZ, l_objMaxZ, l_maxZ, l_objMinZ);

    final float l_minY = l_currentBounds.getCurrentMinY();
    final float l_objMaxY = l_center.y + l_extent.y;
    final float l_maxY = l_currentBounds.getCurrentMaxY();
    final float l_objMinY = l_center.y - l_extent.y;
    final float l_yDiff = evaluateDifference(l_minY, l_objMaxY, l_maxY, l_objMinY);

    final float l_absX = FastMath.abs(l_xDiff);
    final float l_absY = FastMath.abs(l_yDiff);
    final float l_absZ = FastMath.abs(l_zDiff);
    if (l_absZ > l_absX && l_absY > l_absX) {
      ms_tmp1.set(l_xDiff, 0.0F, 0.0F);
    } else if (l_absZ < l_absY) {
      ms_tmp1.set(0.0F, 0.0F, l_zDiff);
    } else {
      ms_tmp1.set(0.0F, l_yDiff, 0.0F);

 private static float evaluateDifference(
     final float p_min,
     final float p_objMax,
     final float p_max,
     final float p_objMin) {
    final float l_diff1 = p_objMax - p_min;
    final float l_diff2 = p_objMin - p_max;
    final float l_ret;
    if (FastMath.abs(l_diff1) > FastMath.abs(l_diff2)) {
      l_ret = l_diff2;
    } else {
      l_ret = l_diff1;
    return l_ret;

ms_tmp & ms_tmp1 are static vectors to avoid object creation. moveableentity is the baseclass for all movable objects in my engine, just use spatial and replace moveabsolute by getlocaltranslation().addlocal() and you'll get a nice and smooth bounding box collision.
by using the "reset to last valid postion"-method, you will get stuck whenever you touch something.

how about the CurrentBounds? what issit ? and this method is called when a collision is detected? the boundingvolume passed in is from p_entity?

and also … if the object is not using boundingbox … so there will be exception in casting … is there a way to stop the player no matter what it collides?  :expressionless:

yes, but it's not simple. actually, collision handling is one of the most complex problems in hhexen.

currentbounds in a helper class for getting the min and max values of the bounding box, you can calculate them easily by ourself.

yes, this will only work for boxes, but per triangle handling is much too complex to simply post a few lines…

p_entity is the player, a projectile, whatever. the given bounding volume is from an obstacle.