Jump to content

What is the best way to implement a collision system between meshes?


DellaFree
 Share

Recommended Posts

Hi guys ! My studying continue, but every new argument I start studying, I find a lots of things that I don't understand well :(

Today, is the turn of the collision/intersection between meshes. So I made this PG: http://www.babylonjs-playground.com/#2AGEE5 (I found it in another topic). I would that every time some sphere collide with a box, decrements its "counter life" by 1, but I notice that it does not decrement by one every time, but sometimes 2 or 3. Maybe, I suppose, that the reason is that the sphere intersects, first, one face of the cube, and then others (based on the trajectory), so it decrements the counter life every time it collides with some face; am I right? So how can I make that every time on sphere collide with a cube, decrement its "counte life" just by one?

And the other thing is that: every time I click, one sphere (bullet) is created, so I need to check if it intersect the cube, and I do that in a if-statement inside the scene.renderLoop(); but if I have a lots of cube, and click very quicly, every frame I should cycle over the box array and check for collision with the sphere, or alternatively, every click I should cycle on the box array and check for collision with the box; but I think that this reduce a lots the performance. I try to use the actions, but with no success (maybe I'm missing something). Is there a way to applay actions to my case, avoiding the continue cycling over the array of boxes?

Many thanks guys.

 

Link to comment
Share on other sites

Hey, that's a cool trick! :-)

 

here is my take on it - http://www.babylonjs-playground.com/#2AGEE5#2

Dispose the bullet once it intersects and then you won't have this problem any more. This will crete a different problem - the bullet can't "shoot" two boxes. But if that's fine then you can use it like this.

 

You have registered a new "before rnder" function with each click, and never unregister them. This will have a serious impact on performance once you shoot a lot. Look at how I unregistered the function when the bullet is too far away or when it intersects.

 

About actions - maybe try showing what you did, this way we can guide you the best.

 

Hope it helps!

Link to comment
Share on other sites

Hi @RaananW ! Great solution! I had never thought something like this. Really thanks.

So for what about the action, here what I made : http://www.babylonjs-playground.com/#2AGEE5#3 . I try to use the action in different places, first , in the line 34 (the line commented),and then in the line 76. There are some comment that explain better the situation. My main goal is to cycle once over the boxes and add, for every box,  an action that check for intersection with the sphere (BABYLON.ActionManager.OnIntersectionEnterTrigger), but when I declare the box, the bullet not exists yet, so it does not work (this is what I've done in the line 34); instead, I try a diffrent way (from line 76): every click , I cycle over the box array and then add an action for every bullet to check for intersection with box; this works in part; I don't know how to modify the property of the box. (I use BABYLON.ExecuteCodeAction).

So, for doing this sort of thing, what's the best approach, for a good performance ( your solution works fine, really :D )?

Again thaks a lot for your support , patience and time,

cheers

Link to comment
Share on other sites

Ahoi!

Babylon allows you to wonderfully use actions only for these kinds of user interactions. Actions ar emuch cleaner - they don't pollute your canvas with unneeded events that are never cleared. They are also much much more organized than this addEventListener chaos functions.

 

http://www.babylonjs-playground.com/#2AGEE5#5

 

This is my take on your code with actions only. There is room for improvement, but hey! it's a nice prototype.

 

A javascript note - avoid using for(var i =0; i < array.length; i++) and use forEach instead. the i variable is constantly changing inside the scope of the function and will not correctly work later. it will only cause problems. forEach works much better in cases like yours. JavaScript is truely wonderful, but sometimes you just have to do things like IT wants and not like you want :-)

Link to comment
Share on other sites

Hi @RaananW! Great! It works fine. Thanks for the advice. Now I'm going to adjust my code (regarding the for cycle and other things), and I will make a playgound with all and, I hope soon, I'll share with yours, for suggestion/tips/help.... Do you think it could be a good idea? This is a project for an university exam (it's very simple); 2 months ago I didn't know nothing about Babylon.JS (maybe , even now I don't know nothing about itXD, but I'm so happy to have learned a lots of things). So do you thinks it could be a good idea to improve my "skill", or is better to open different topics?

Again, thaks for your time and support,

cheers

Link to comment
Share on other sites

The forum is here exactly for this kind of questions and thoughts! and the users are eager to help. Really :-)

Start new topics if you have other questions,maybe first try seeing if anyone had the same problem like you using a simple search.

The doc site is also a nice way of learning new things, check the tutorials section.

Link to comment
Share on other sites

Yes, I'll do it. I mean to make a PG that summarize my project,also for simple opinions. I think that it easier to check out how to make some features better. So you suggest to open different topics, instead only one? (Yes I searched all day on this forum, I also read your article about the actions )

For the last 2 months, I've been on this forum every day :) it 's simply amazing.

P.s

Sorry I forgot to make this post resolved :(

Link to comment
Share on other sites

Hi ! I'm still playing with actions, and I would ask some questions (here is the PG : http://www.babylonjs-playground.com/#2AGEE5#9 ):

my goal is that when all the boxes are disposed, I woud recreate a new one after 5 seconds; I try to make it in different ways:

- the first is that OnEveryFrameTrigger I check if the boxList.lenght is equals to 0, and if it is true I create a new box, but in this case the setTimeout doesn't work;

- the second (the commented lines from 63 to 74), I use the .then function and use the same action and the same trigger but , in this case, I add a condition (true if the lenght of box list is equals to zero);but this solution doesn't work; I think that only the first action is considered.

Any idea on how I can do that? So this function will become later a system for a sort of multilevel (after you destroy all boxes, start a new level , after some seconds, with other boxes, and so on).

Thanks again guys,

cheers

Link to comment
Share on other sites

Hi Deltakosh. Thanks for your replay. Your solution works fine :D But I notice that after the cube is created, the browser crashes. Maybe it's only my problem :( .On firefox I get this error:

 

A script on this page may be busy, or it may have stopped responding.
You can stop the script now, or you can continue to see if the script will complete.

Script: http://www.babylonjs-playground.com/index.js line 207 > eval:95

 

Instead in other browers (Chrome and Explorer) I should shout down the process and re-open it. What could be the problem?

Really, thanks for all :D

cheers

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