Jump to content

CanonJS Physics: Pass through certain objects


Recommended Posts

I'm trying to figure out how to make a projectile pass THROUGH certain objects while making contact with others. I'm also having the issue where my projectile is created dynamically so I can't stick in a event listener in the registerBeforeRender() function because they don't exist at the time that function is called. Basically, I want to be able to shoot enemies in my game, while not shooting allies.

I'm also having an issue where I can't seem to get rotation coordinates of my object. I can get the coordinates but when I check mesh.rotation() I always see 0, 0, 0 even when I change it with a event listener...


Link to comment
Share on other sites

Are you using the impostor system or native cannon.js?

Cannon.js uses quaternions for rotations, so your mesh probably uses a rotationQuaternion.

I think what you want is collisionFilterGroups and collisionFilterMasks. With these, you can say:

World objects, floor, walls etc.has group 1.

Team 1 has group 2.

Team 2 has group 4.

Team 1 Bullets have group 6

Team 2 Bullets have group 8


All your world objects/map objects like barrels, walls, boxes etc. should be created with:

collisionFilterGroup: 1,

collisionFilterMask: 2 | 4 | 6 | 8

This makes them collide with all groups.


Now, whenever someone on team 1 creates and shoots a bullet, create the body with the following:

collisionFilterGroup: 6,

collisionFilterMask: 1 | 4 | 8

The body of the bullet from the player on Team 1 will now only collide with groups 1,4 and 8. That is the world objects, Team 2 players and Team 2 players' bullets.


Whenever someone on team 2 creates and shoots a bullet, create the body with the following:

collisionFilterGroup: 8,

collisionFilterMask: 1 | 2 | 6

Same thing as above.


Why do you want to add the eventListener in a loop?

As soon as the body is created, you can register the eventListener, and it will register any collision. You can(and should) register it in the same function you use to shoot or create the bullets.

Now, when you want to remove the bullets from the Cannon.js world, Cannon.js will crash if you do it right away, as it's not completely done doing calculations with the body.

So instead, put it in an array of bodies to be removed, and before you run the world.step function, loop through this array and remove all bodies in it.


Link to comment
Share on other sites

Hi W.  Here is a basic CannonJS physics scene... in the BabylonJS playground.


Feel free to make changes, do more runs, make new saves, grab zips.

Line 37 (thru 41) is the where the physics impostor is attached to the mesh.

A playground SEARCH for physicsImpostor returns a big list of other demos, too.

Hope this helps.

Link to comment
Share on other sites

Is there a way to use the filtergroups with imposters? I have the physics working and collision is detected using imposters and it seems using CANNON body objects is a bit more complex. (creating a world, adding objects to world etc)

My rotation problem is solved.

Thanks for the help I really appreciate it.

Link to comment
Share on other sites

Hi again, W.


I've never used them, but there's some playgrounds that have.

Actually, if you use the BabylonJS physics plugins for CannonJS and OimoJS, they are both used in (mostly) the same ways. 

There ARE differences, but our plugins coders have tried to make the two systems reasonably mirror each other.  Oimo doesn't have meshImpostor or heightMapImpostor, but other than that, not too much difference.  Both use the same impostor and joint installation methods, both use the same linear and angular velocity setters and applyImpulse system.

It is mostly when we "go native" that some differences are seen (when we use native Cannon or Oimo calls).  The few physics pros that we have around here... have done a pretty nice job with the plugins "layer of abstraction" mirroring.  It COULD be LOTS worse.  :)  Compare the Cannon plugin with the Oimo plugin, as wanted.  There's not too much difference.

Our physics Engine, Impostor, and Joint wrapper-classes are common to both engines, so, no differences there.

Is there something in particular... that makes you say that Cannon is more complex?  Were you talking about native calls directly to the physics library?  There could be some substantial differences in those areas... I haven't studied them well.  I use the plugins, but "going native" is certainly an option... for both engines.

Teach us what you learn about that stuff, okay Wemperer?  :)  Thx!  We can use all the physics help we can get.  We have a slight shortage of physics experts.  :)  Hope this helps.  Others may comment soon.  Be well, party on.

Link to comment
Share on other sites

Sorry to bother once again but I have to come back to my original question. How do I check the rotation of an object? The rotationquartions isn't working for me, I just do not understand the math (im not a math person). I need things in radiant/degrees. When I use:  

player.rotate(BABYLON.Axis.Y, BABYLON.Tools.ToRadians(1), BABYLON.Space.WORLD)

I need to figure out what property of the mesh this is rotating so I can convert it to Radians. If I attempt to convert rotation.Quartions I get NaN. player.rotation always returns 0, 0, 0. I


I swear when I figure this out I'm writing some Idiots Guide level tutorials...

Link to comment
Share on other sites

:) No bother at all. 

Quaternions are not friendly.

Most programmers use mesh.rotationQuaternion.toEulerAngles()

This will return rotation values similar to mesh.rotation.

(make sure you spell 'Quaternion' correctly when you code)

Note:  mesh.rotationQuaternion property is not initially installed on BJS mesh.  It is installed as needed, often by applying a physicsImpostor, or by using the .rotate() command like you have used.  Hope this helps.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...