Collision of 2 physical bodies

There are 2 physical bodies that move at linear speed. How can we determine their collision?

Hi B. Are you using a physics engine? We have interface for OimoJS, CannonJS, and AmmoJS.

Here’s a playground… using CannonJS physics engine… with sphere.physicsImpostor.onCollideEvent = myCollide; providing color change and console report… each collision with ground. No collide report from ground… only from sphere.

https://www.babylonjs-playground.com/#1IB3JT#12

Here’s one with two spheres.

https://www.babylonjs-playground.com/#1IB3JT#14

No collide reported from ground or sphere2, just from sphere1… but you can easily see how to activate a collision report from sphere2, as well.

If this isn’t what you want… PLEASE… give more details about what is wanted. Thanks.

2 Likes

PG: https://playground.babylonjs.com/#8ULU6G#12

I need to make the machine respond to a collision with other bodies (output of any message for example)

1 Like

nod. Invisible box around car… tiny bit larger than car… with impostor… should work fine. Keep box off-ground perhaps, to avoid continuous collider ā€œscrubbingā€ against ground.

Nice scene. thx for details/demo… cool.

But the car doesn’t respond to objects. There is physics, but how to set a callback in a collision?

https://www.babylonjs-playground.com/#1IB3JT#14 - line 54… onCollide function?

Could use onCollide to (opposing) applyForce on the car… if you can fig collide direction/angle.

Put big mass on those collided obstacles, and lower mass on car with restitution on both… I promise car will stop dead and recoil and do all sorts of hell. Mass-up the obstacles. (might work, no promises) Right now, they move too easily… not enuf friction and mass and resti.

If you put an ammo plugin in your PG the collision event stops working. My сar uses an ammo plugin

ahhh, well that’s the first thing to work-on, then. Possibly broken AmmoJS plugin. hmm. Wingy tests.

Meantime, maybe we both need to read… https://forum.babylonjs.com/t/ammo-js-collision-response-and-filtering/

Advanced AmmoJS collision crap… with forum friend @MackeyK24 . I’m far from AmmoJS expert… barely introductory levels. Let’s also beg for help from @Cedric, @RaananW, @Raggar, @trevordev and whomever else we can bother about Ammo. :slight_smile:

Not many web-docs about Ammo. Sort of need-to use Bullet docs.

Ahhh… here we go…
https://www.babylonjs-playground.com/#1IB3JT#17

Line 51… a little different way of checking for collide. I’m seeing bounce reports @console.

YAY! (fingers crossed that it will work for 11116)

[Wingy removed lines, here.]

1 Like

Will not work :slight_smile: I tried this method. The fact is that I didn’t directly declare physics as the body of my car

It doesn’t has physicsImpostor

So… the wrap-the-car-in-invisible-physics-active-box… a possible option?

2 Likes

I tried to install a physics handler on a box on top of which the car body lies, but in this case the car broke down.

Yeah, it needs to be opposite of that… parent a new invisible box to car core… and make invisible box same size as car… + a tiny amount, and lifted off-ground a tiny amount… perhaps. Not sure. Keep experimenting.

In your case, perhaps set physicsImpostor on invisi-box first, and then parent it to car core/root mesh. Doing it in opposite order… might make things go wonky. I THINK… our physicsPlugins do special handling… when an impostor is added to a mesh that has a parent.

SO, maybe try impostoring first, then make car root be its parent. Then maybe adjust position of invisi-box… cuz it may be ā€œoffā€. Not sure. Maybe others will comment soon. Invisibox.visibility = .01 … then invisibox.showBoundingBox = true; should work (Helps for after-parenting invisibox positioning, perhaps.)

I’m sorry, I can’t tell what the problem is. What exactly is your problem or what can’t get working?

Mackey… my fault for pinging you. I thought he/she might have an issue with Ammo collision masks/groups… which you worked-with for your racing game/stuff.

But then I learned that he/she had no impostors on the car at all… and after that… I got all confused and realized collisionGroups were not the issue at all.

Mis-ping on my part, sorry… but thanks for visiting and trying to help… certainly allowed and welcome. (hug) Now I’ll shut up. :slight_smile: Hope you’re well.

PG: https://playground.babylonjs.com/#8ULU6G#17

I tried adding a physical box on top of the machine, but it doesn’t work
Lines: 265 - 279

Ahh, this is an Ammo RaycastVehicle… so its full of physics already. You won’t be able to enclose it within the volume of a surrounding-box that’s also physics active… they will jettison each other into outer space. (impostors HATE overlapping each other, and can turn violent) :slight_smile:

Yuh, this vehicle is already packed with physics. No surrounder-box allowed here.

Ok, did you try setting high mass (and maybe restitution> 0.9) on all the obstacles and then go driving into them… see if you could make the car bounce-off?

Completely disregard my surrounding-box idea… won’t work here. This car IS physics active already, without needing to set it so. It is a special AmmoJS class ā€œthingā€ā€¦ fully physics-hot by default.

And what to do in such a situation? :slight_smile: Maybe RaycastVehicle has its own methods for this?

Likely. Did you try setting hi mass on the obstacles, yet? Perhaps masses in the area of 5-20.

Restitutions of 0.9 on them, too, maybe. At least 0.5 restitution… for good car-rebounding (bounce-off the obstacle). Must have both, though. Obstacle should be HEAVY (big rock)… and sometimes ALSO plenty of restitution… to send the car flying after impact.

Car itself might have ā€œover-allā€ restitution setting too, and mass setting… and special wheel friction. Often not needed on each piece of the car… but possibly params during MAKING of car.

Possibly in the… var tuning = new Ammo.btVehicleTuning(); stuff.

Likely in code lines between // going native and addWheel() function. There is a LOT of ā€œbuild and adjust the carā€ in that area… most of which could affect how the car interacts with obstacles of various mass.

Ya need about a week of study… for THAT section of code… because the Ammo raycast car is complex… to make it realistic. Its no child’s toy… it’s right up there in the level of ā€œaccident reconstruction softwareā€ā€¦ beefy and powerful.

Ever drive a sim called BeamNG? The Ammo raycast car is NEARING that level of complex/realistic, likely. Not a toy/game. A car simulator.

WAY over my head. Let’s listen for experts. I bet I have already said something incorrect to you. I am no expert at physics THAT complex… not even close. (sorry)

2 Likes

PG: https://www.babylonjs-playground.com/#102TBD#185

I don’t understand how this piece of code finds collisions, but it works (I remade a bit to fit my needs):

var physicsWorld = scene.getPhysicsEngine().getPhysicsPlugin().world;

function collisionCallbackFunc(cp, colObj0, colObj1) {
    const bodyIndex = array.findIndex(i => i.physicsImpostor.physicsBody.ptr === colObj1)
    colObj0 = Ammo.wrapPointer(colObj0, Ammo.btRigidBody);

    if (colObj0.isCar && array[bodyIndex] && array[bodyIndex].isOtherBody) {
        array[bodyIndex].dispose()
        array.splice(bodyIndex, 1);
    }
}

var collisionCallbackPointer = Ammo.addFunction(collisionCallbackFunc);
physicsWorld.setContactProcessedCallback(collisionCallbackPointer);
2 Likes