Jump to content

Regarding multiple meshes and detecting the individual collisions


tr00n
 Share

Recommended Posts

Hi guys,

I've been working on getting a basic prototype up and running for Dialo'esque game. All is well and I'm now at the part where my 'character' has to interact with objects laying around in the world. The way that I've done is that I create multiple random cubes and position them randomly. When my character touches each of these objects I'd like for it to execute some custom code for that particular object. 

 

The code that I'd like to execute could be something like increment food points but also at the same time inactivating that object. Thus I collide with it again it should trigger no response.

 

Now, I've had success experimenting with the action manager and all works well with a single object. However when I create many of the same object it only seems to work once.

 

It seems that all these objects share the same parameter (is_used) and that it doesn't get instantiated properly. In Three.js I was able to set this up per instance of the object and then I was able to turn off the is_used boolean for a particular instance of that object.

 

I will continue digging for a solution but any input is welcome :)

 

This is the code that defines a single of these randomly dispersed objects:   

       function foodRation(size){            var box = BABYLON.Mesh.CreateBox("box", size, scene);            box.material = new BABYLON.StandardMaterial("Mat", scene);            box.material.diffuseTexture = new BABYLON.Texture("/assets/ground.jpg", scene);            box.material.diffuseTexture.hasAlpha = true;            box.position.x = getRandomInt(3, 20);            box.position.y = getRandomInt(3, 20);            box.is_used = false;            box.collisionsEnabled = true;            box.checkCollisions = true;            box.actionManager = new BABYLON.ActionManager(scene);            var collisionTrigger = { trigger: BABYLON.ActionManager.OnIntersectionEnterTrigger, parameter: meshPlayer };                        var collisionFunc = function(){                if(box.is_used==false) console.log("code that adds food to character");                box.is_used = true;            };            box.actionManager.registerAction(new BABYLON.ExecuteCodeAction(collisionTrigger,collisionFunc));       }
Link to comment
Share on other sites

Hi Deltakosh,

Thanks for your reply. Whilst I was recreating it in the playground repro I actually found out what my problem was. It was a simple typo where I had accidentally declared the Y position rather than the Z position of the boxes:

box.position.x = getRandomInt(3, 20);box.position.y = getRandomInt(3, 20);

My camera is orthographic so I didn't notice that the boxes were in fact on top of each of other. 

Now that I've changed it to Z rather than Y it all works beautifully. 

Sometimes its those simple things but I can't but feel a little silly :)

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