🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Collisions

posted in trill41 for project ABx
Published January 29, 2019
Advertisement

Collisions have been a constant source of frustration, maybe because I decided to use my own Math library, and did not just use Bullet or something. The game does not have full physics simulation, so I thought a physics engine would be overkill, and how hard can it be to write some collision checking code? Well, at least it's not really easy.

Collision shapes

For now I have the following collision shapes, which can be checked against each other (except Height map vs. Height map does not work):

  • Axis aligned Bounding Box (AABB)
  • Oriented Bounding Box (OBB)
  • Sphere
  • Height map
  • Convex hull

All dynamic objects (like Players, NPCs) have an AABB while static scene objects (buildings etc.) have an OBB or Convex hull.

AABB

As the name suggests, an Axis aligned Bounding Box is parallel to the coordinate axes, regardless of the orientation of the object it wraps.

AABB.png.8fbb355acf7651345547c0662c391261.png

OBB

An OBB has the same orientation as the object it wraps.

OBB.jpg.f7887cdeba4ca3d9adfb3cfbcd304e42.jpg

Loading the Scene

The scene is made with Urho3Ds editor and saved to an XML file. The Server loads the same XML file with simplified models or just Bounding Boxes. Since the Server does not have something like a Scene Graph, but just a flat array of Game Objects, the loading method differs a bit to the one in Urho3D.

Scene Viewer

So when running against invisible walls or running through walls, there must be something wrong. It could be that:

  1. My Scene loading routine has bugs,
  2. the simplified models are wrong,
  3. or the collision checking algorithms have bugs.

To find that out, I made simple Scene Viewer built into the Server. I realized that only with guessing and trial and error, I will not be able to track it down. Bellow you can see the objects on the Server:

SceneViewer.png.382e7aa2f30b40eb88afa2d130c8dda7.png

There are still problems with different coordinate systems. My Math library was made for DirectX, but the Scene Viewer uses OpenGL with the help of freeglut and glew. One reason for this is, that the Server should be cross platform, at least Windows and Linux, and it's easier to make a simple 3D viewer with OpenGL than with DirectX. Anyway, now I get an impression of what's going on on the Server.

To be able to draw the Collision shapes, they must be converted to vertex and index data which form triangles. For example, to generate vertices and indices for a Bounding Box, I use the following:


Shape BoundingBox::GetShape() const
{
    Shape s;
    const Vector3& boundPoint1 = min_;
    const Vector3& boundPoint2 = max_;
    const Vector3 boundPoint3 = Vector3(boundPoint1.x_, boundPoint1.y_, boundPoint2.z_);
    const Vector3 boundPoint4 = Vector3(boundPoint1.x_, boundPoint2.y_, boundPoint1.z_);
    const Vector3 boundPoint5 = Vector3(boundPoint2.x_, boundPoint1.y_, boundPoint1.z_);
    const Vector3 boundPoint6 = Vector3(boundPoint1.x_, boundPoint2.y_, boundPoint2.z_);
    const Vector3 boundPoint7 = Vector3(boundPoint2.x_, boundPoint1.y_, boundPoint2.z_);
    const Vector3 boundPoint8 = Vector3(boundPoint2.x_, boundPoint2.y_, boundPoint1.z_);

    // Add the vertices
    s.vertexData_.push_back(boundPoint1);
    s.vertexData_.push_back(boundPoint2);
    s.vertexData_.push_back(boundPoint3);
    s.vertexData_.push_back(boundPoint4);
    s.vertexData_.push_back(boundPoint5);
    s.vertexData_.push_back(boundPoint6);
    s.vertexData_.push_back(boundPoint7);
    s.vertexData_.push_back(boundPoint8);
    s.vertexCount_ = 8;

    // Create the triangles
    /*
        5------------1 max
      / |          / |
    3------------7   |
    |   |        |   |
    |   2------------6
    | /          | /
min 0------------4
    */
    // Front
    s.AddTriangle(7, 4, 0);
    s.AddTriangle(0, 3, 7);
    // Left
    s.AddTriangle(0, 3, 2);
    s.AddTriangle(2, 5, 3);
    // Right
    s.AddTriangle(4, 7, 1);
    s.AddTriangle(1, 6, 4);
    // Up
    s.AddTriangle(7, 1, 5);
    s.AddTriangle(5, 3, 7);
    // Down
    s.AddTriangle(2, 6, 4);
    s.AddTriangle(4, 0, 2);
    // Back
    s.AddTriangle(6, 2, 5);
    s.AddTriangle(5, 1, 6);

    return s;
}

The same scene as it the Client sees.

ClientView.thumb.jpg.f74db193a912df477ceedf6776abc28e.jpg

It turned out to be a combination of all three bugs (of course). Most of them are fixed now, but I still have problems with OBBs.

Of course, the Server still can be compiled without the Scene Viewer because it impacts the performance of the server. The Linker will drop then all references to OpenGL too.

Previous Entry Skills
Next Entry Gameplay
2 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement

Latest Entries

Advertisement