Don't walk through the objects

Hi!



I have a new problem and I think it should be very common. I have an animated model inside a room and there are other models such a table. I can keep my model inside the room changing his local position when it's near a wall. However, it walks through the other models. I would lke to stop the model when is close to the table but I don't know how to do it.



Thank you.

Thank you, but I can't understand it at all.


no suprise there. the human brain is not made for reading code. especially not mine. it's made for understanding stuff by imagining it.

this is a piece of code copied & pasted from my game. what it does:
it checks if the left side of bounding box a is "more left" than the right side of the other box. if yes, it calculates the difference and moves one of the objects back so they don't intersect. same for all dimensions/sides of the boxes.

what currentbounds is: it's a class holding the current bounds' edges and size because i didn't like to calculate the edges of the bounding box or sphere or whatever it might be every time i need them.
simply replace decoratedentity with a node. currentbounds.minX, maxX and so on are the bounding boxes top, left, bottom etc. coordinates.
in case i forgot something: just guess. your first idea will be the right one. i choose my class, members and method names based on the idea that i will someday forget what the code is supposed to do and how it does it, so i can read the names and at least know what they do and what they are without having to understand the code (near impossible task for me)

low budget solution:

use a fixed framerate. when both objects collide, reset the actors position to its previous one.

better solution:

private static void adjustEntityPosition(final DecoratedEntity 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);
      if (l_yDiff >= 0.0F) {
        p_entity.setTouchingObjWithFeet();
      }
    }
    p_entity.moveAbsoluteBy(ms_tmp1);
  }

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



this piece of code does exactly what you want. when 2 object's bounding boxes intersect, it takes one of them and pushes it back so its perfectly aligned to the static one. this also automatically leads to slower movements when walking towards a wall in an angle between 0 and 90 degrees instead of simply stopping.

Thank you, but I can't understand it at all.

First of all, what's a 'CurrentBounds'?

I want to do it in the easiest way. I have to Nodes (with BoundingBox) and one of them is static.

How can I do it?

There is a second way for collision detection, using rays. You shoot a fan of three or five rays in the direction you want to move and stop if one of the rays hits something which is too close. This technique can be cheaper than the other approach, but is not so "robust" and obviousely complex or "spiky" objects can be problematic.