How to prevent movement (simple collision detection/no physics)

Hi all,

I’m working on simple collision detection. I just need things to be solid I do not need physics.
My goal is to have a ‘solid’ wall that my model can’t pass through (both Spatials)

I am using this TestTriangleCollision source code.

On line 122 we are able to detect a collision and change the color of the box (in this example a geometry) to red
geom1.getMaterial().setColor("Color", ColorRGBA.Red);

I was also reading this book at work today and they are doing some collision detection and stop a flappy bird in it’s tracks with this line of code (full code segment at the bottom of post)

timer.stop();

So this got me to thinking-
With just a line of code in each of these examples a pretty cool outcome occurs when the collision is detected. Is there a method/simple way that I can halt my model in it’s tracks so that it doesn’t go through the wall?

I don’t even know if this is possible because then I also think well if I stop moving when I hit the wall, will I just get stuck and then not be able to move anywhere? I’ve been trying some things but no luck yet.

Anywho any comments, suggestions, or thoughts would be most appreciated. Thanks!

Flappy bird code segment
...
private void timedAction() {
...
// check for collision
Wall firstWall = walls.get(0);
Rectangle birdBounds = bird.getBounds();
Rectangle topWallBounds = firstWall.getTopBounds();
Rectangle bottomWallBounds = firstWall.getBottomBounds();
if (birdBounds.intersects(topWallBounds)
|| birdBounds.intersects(bottomWallBounds)) {
timer.stop();
}
...

When a player walks into a wall, the natural thing you would expect to happen is for the player to come to a complete stop if they walk into it straight at a perpendicular angle, or if they walk into a wall at a slanted, non perpendicular angle then they usually slide along the wall in the direction they were trying to walk.

So to do that you need to use the direction that the wall is facing to figure out what direction to move the player when they hit walls at different angles.

So when you do a colision with CollisionResults, you can use the getContactNormal() method to get the normal vector of the triangle that you collided with, and then you can cross that value with the direction the player was moving to get the direction that they should slide along the wall they’re walking into.

In case you aren’t sure what the contact normal is: Every model is made up of a set of vertices where each vertex has a position Vector as well as a Normal vector, and a vertex’s normal vector represents the direction that vertex is facing

The only issue with the test case you linked is that they are doing Geometry to BoundingVolume collisions, and the only way to get a single and accurate closest-contact point and contact normal from the CollisionResults is when using a Ray against any other Collidable (since a ray is a single line it can always return one single collision point). So this is why I personally use 2 rays (one facing down to determine altitude, and one facing the direction the character is about to walk to check for collisions every frame) to simulate lightweight physics for a moving character in my game, rather than a bounding box.

1 Like

I actually made a mistake with this part, I wasn’t at my computer to reference my code but crossing the vectors isn’t necessary at all, IDK why I thought I did that somewhere. You can just move the character in the same direction as the contactNormal you get from the ray collision (normalized and multiplied by the players speed and tpf), and that is enough to make the player slide along the wall in a natural way, instead of just stopping them altogether. Sorry if my previous post made things more confusing, I should have waited to post until after I checked my code to make sure I was explaining it accurately.

1 Like

Ohh yes that’s right geometries have a few stipulations.

No worries at all donuts thanks for the help! reading anything is helpful to just understand the concepts and possibilities even.

I’ll continue messin around!

1 Like

That comment made me laugh. (it is how games are coded), but I was thinking when was the last time you ran into a slanted wall walking straight you you started to slide along with you. You hit it and either came to a dead stop (no matter the angle) or bounced off or fell over. But games, the walls are slick to slide on them.

2 Likes

Heheh.

In real life, I’m also not floating in giant invisible capsule with a non-newtonian intertial drive on my belt. :slight_smile:

3 Likes