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

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.

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 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. Hope youāre well.

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)

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? 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

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