Jump to content

Issue with intersecting meshes on initialization


cnjlemm
 Share

Recommended Posts

I am new to babylon and am creating a space game based on gravity and mesh intersection. Basically, my game states are being managing and determined based on if a ship mesh is intersecting with a destination or obstacle mesh. Looks something like this:

 

  scene.registerBeforeRender(function()
  {  
    if (ship.canvasObject.intersectsMesh(canvasObjects[0].canvasObject, true)) {
      ship.material.emissiveColor = new BABYLON.Color3(0, 1, 0);
      PubSub.publish('COLLISION EVENT', 'collided')      
    }
 
    for(var i = 1; i < canvasObjects.length; i ++){
      if (ship.canvasObject.intersectsMesh(canvasObjects.canvasObject, true)) {
        ship.material.emissiveColor = new BABYLON.Color3(1, 0, 0);
        PubSub.publish('COLLISION EVENT', 'collided with other stuffs')
      }
    }
 
Where ship is a prototype. When the page loads, it says that the meshes are already intersecting once, then they stop intersecting, and then they intersect again. The positions of each are (-20, 1, -20) and (25, 1, 25). I feel like there's something simple-ish here that I'm missing and I've gone through all the docs but can't figure it out. 
 
Any help appreciated!
Link to comment
Share on other sites

Hello, 

 

When you update the position, actually nothing is done by Babylon: mesh positions are not *really* updated. 

Positions are updated when the world matrix of the mesh is updated: this step is done at the very first render.

That's why you have this behaviour: 

- collision at first frame

- render

- world matrix updated

- no more collision

 

To fix your issue, you have to force the first update of the world matrix, like this: 

myMesh.computeWorldMatrix(true);
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.

Guest
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.

Loading...
 Share

  • Recently Browsing   0 members

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