Check collisions between two specific objects

Hi everyone,

I tried researching through the manual and API like usual for any tech question, but couldn’t find anything about using the Navigation objects (not full on realistic physics, just simple player controlling) to check if it collides with a specific object, like only enemies and not any other object, and tried searching “collide” in the scripting API like one should usually do if they can’t find any help in the manual, but could only find scripts about colliding with the world as a whole, and not with a particular object like an enemy or coin.

Does anybody know of the proper script to check for a third person player character colliding with a specific object, or maybe any specific object colliding with any specific object in general so I can use it for more objects than just a player character?

Even though I am not using full on physics with realistic rolling and stuff like that for a simple player character (no rigidbodies at all, 100% percent through and through please no), does anyone with experience know if the player navigation can inherit the ability to check “On Collision Enter” or “On Collision Exit” from the rigidbody menu, or does it not work like that?

What you describe is exactly done by physics engine, that provides messages about when 2 things collide, see examples/physics/physics_2d_collisions/ example.

From your description, it seems you don’t want to use physics (though you don’t specify why). If you want to check for collisions between 2 objects without physics, just compare their bounding boxes, like

IsCollision := Scene1.WorldBoundingBox.Collides(

( This will not be efficient if you will have lots of pairs to check – which is why physics engine is more efficient at this when you need to check/consider many possible collisions. )

I know it sounds weird at first if you don’t read the post carefully, but I want to not use the physics engine for my player navigation because I want to make a simple third person character model that does not bounce or roll over like a realistic human model would, just walk around, jump, fall, attack, die etc like Super Mario 64 style (great description of what I am trying to get - simple 3D platformer physics, not realistic rolling or bouncing or anything like that).

The reason I want to make a simple navigation style and not use the realistic physics engine is because i want to save on RAM and processing needed/code, but is it possible to use physics in a way so that the objects do not roll or bounce at all, but it supports everything else like gravity?

There are a number of ways to limit how TCastleRigidBody is actually affected by physics. See Castle Game Engine: CastleTransform: Class TCastleRigidBody .

  • LockRotation allows to prevent object from being rotated by physics. Set it to [0,1,2] to prevent object being rotated at all by physics.

  • LockTranslation allows to prevent object from being translated (moved) by physics in a specific axis.

  • Trigger means that objects will pass through this object (it will not cause other objects to bounce off).

  • Dynamic can be turned off to completely disable the object from being moved/rotated by physics (this includes all collisions and forces, including gravity).

Experiment with them :slight_smile:

P.S. Don’t get me wrong, if you want to only do sporadic check for “3D object vs 3D object” collision, for a few specific pairs, then maybe indeed using physics will be overkill.

Using my snipped to just do IsCollision := Scene1.WorldBoundingBox.Collides(Scene2.WorldBoundingBox); may be reasonable approach, I’ve done such thing in my own games many times. No need for physics, just do this simple test in each Update.

But if you want to check more collisions, or you want gravity anyway, then using physics may be both easier and more efficient. That’s why I recommend to test this approach. Don’t be afraid prematurely that physics will eat your RAM or CPU without testing – it depends on the exact case (how complicated physics world with inter-connected colliders you will use), but physics can be completely cheap on CPU. And I never observed it having much effect on RAM.

Okay, that is very helpful to know. Thank you so much!

I also assumed, that any box I used would bounce naturally on contact, and I know you showed me how to disable something as something to bounce off of, but do you know if there is a feature for the inverse (preventing something from bouncing off anything period, so that I don’t have to keep setting flags/code to prevent things from bouncing off them, or sometimes the solution won’t work if I only want some things to bounce and not others, particularly because the code prevents the bouncing full stop with no compromise?)

The code I am referring to is the “Trigger code” you described in more detail above.

Hello everyone,

I think I figured out how to do it without needing a specific type of code for it (like “Trigger” as mentioned above), I just get the velocity on collision with whatever, and apply the inverse/inverted version, so that the net result should be no speed or movement. Again I haven’t tested RigidBodies yet in Castle Game Engine, I just know that even simple cubes will bounce off every surface unless told otherwise in other engines like Godot

Or actually, now that I think about it further, velocity might not be the best because usually collision bounces accelerate rather than having a constant velocity (cause a force causes acceleration, basic physics 101), so instead I would apply a force in the opposite direction of the bounce force, but the principle still stands, that if there is no code to disable an object bouncing off anything and everything using a boolean true/false or checkbox, I can simply apply the effect in reverse and it should cancel out because of how vectors and physics works.

It does look, unfortunately, like that there is no way to retrive/ask for an acceleration or force like you can with velocity, only apply a force.

Does anyone know then, how one would get the strength of the bounce force so that you can apply the correct amount of force to cancel it out?

Oh wait, I think I figured out my own question now… the simple third person navigation example does not use a navigation mesh for movement and no rigid body, like I thought before, but it actually uses a rigid body with the rotations and things like that turned off.

So never mind, maybe it didn’t use rigid body in the last download version I had, but now I can see clearly it uses rigid body in the way you mentioned, so I believe there is no excuse not to use it now, and there is no need to cancel bounces because it doesn’t bounce by default like I thought.

“Acceleration” is not a persistent state tracked for a rigid body. Physics engine only tracks velocity, the rest (forces) is applied immediately to velocity and “forgotten”. Well, actually Kraft tracks RigidBody.fForce within the current step… but it gets zeroed at the beginning of next step anyway.

We also don’t get simple information “force that is the result of this contact”.

What you describe, trying to “cancel out” a collision by applying inverse force, sounds quite “hacky”. If you want the collision to be ignored, then the best (simplest and reliable) approach is to tell the physics engine to ignore it :slight_smile:

Triggers and layers, that I mention below, are a way to do this.

This is also trigger. It should work for dynamic objects as well.


I hope to merge this weekend (or maybe early next week) a PR that also adds physics layers ( Physics layers by and3md · Pull Request #467 · castle-engine/castle-engine · GitHub ) so that you can control better what collides with what, even without trigger (since trigger is like “ignore all”, with layers you have more fine-grained control of what collides with what).

General note:

You’re trying to solve some bigger task, but I don’t know what it is, which is why I’m just guessing about the optimal solution for you. To get best help, please describe your “bigger problem / task”. I.e. what kind of game you make, what objects you have, what collisions you want to detect (and how you want to react), what collisions you want to ignore. We can advise best when we know the “big picture” of the problem :slight_smile: Without knowing the “big picture”, I cannot even be certain if using physics is optimal for you, or if doing a few tests “Scene1.WorldBoundingBox.Collides(Scene2.WorldBoundingBox)” is best :slight_smile:

So, I’d advise to describe your “bigger problem / task”, like “I want to detect when player picks up an item by walking into the 3D box of the item” or such.

Basically the bigger problem is exactly that, my biggest/ultimate goal is to create a simple old school 3D platformer like Super Mario 64, as mentioned above, so I just want a more simpler physics engine like that, and not something that rolls over realistically GTA 5 car style.

And I thought it wasn’t possible to do that, using just rigid bodies, but you made it clear in your help that it can be done, especially using layers to control what exactly collides with what, so that it doesn’t ignore everything period (which is not what I want, I want it to touch specific objects and pass through others, exactly like in Mario 64). You’re saying though, that you’re working on layers and it’s not actually in the downloads page engine yet?

Good news: The physics layers have been merged on Tuesday, and they are available already in the builds on Download | Castle Game Engine :slight_smile:

There will be a news post (see our news: Castle Game Engine – Open-Source 3D and 2D Game Engine ) about them around this weekend. For now, check out examples

  • examples/physics/physics_3d_collisions_layers ,
  • examples/platformer (more involved).