Jump to content

Collision Detection


Recommended Posts

Hello Everyone,

I just started working with Babylon JS a few months ago and so far it is great. Right now, I am working on a project where if you click on any given point the selected unit will move to that spot. What I need now is to be able to tell when a mesh collides with another mesh while it is moving to that spot(see video). Any ideas how I could do this? Is there a way I could call a function on_collide? I would greatly appreciate any help.


Link to comment
Share on other sites

3 hours ago, Flomotion said:

I think you can use mesh.moveWithCollisions(speedvector) for that.

In reply to Flomotion.

Thanks for your reply. After looking into moveWithCollisions it looks like that might work. I have a couple of questions about it though.

Normally I would create an animation to move a unit like this (shortened version):

var positionAnimation = new BABYLON.Animation("testAnimation", "position", 150, BABYLON.Animation.ANIMATIONTYPE_VECTOR3, BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT;

keys.push({frame: 0, value: positionVector1});

keys.push({frame: 40, value: positionVector2});

keys.push({frame: 80, value: positionVector3});

I would then play the animation to move a unit.

From what I could see, it looks like the moveWithCollisions function will just move in a direction and not stop until it reaches an obstacle, any way you can just give it a point to move to? Also how do I control the speed with which this moves the mesh?

If you could just point me to a link with the docs for this function that would also help but I couldn't find it on the Babylon JS site.

Thanks again for all your help,



Link to comment
Share on other sites

In reply to iiceman.

Thank you for your reply as well. That plugin looks really cool I would probably use it if I was starting from scratch. I probably will just go with moveWithCollisions for now just because I already have a pathfinding plugin based on a grid that computes paths pretty fast, I just need moveWithCollisions for running into other units.



Link to comment
Share on other sites


I understand what you want to do. I guess you have to calculate the angle of the end position for each mesh and check in the registerBeforeRender function if that position was reached. Meanwhile make the mesh move in the direction of the end point with a certain speed.

I only used it to make an object move in the camera direction constantly.

See section 4 of this tutorial


Hope it 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...