Jump to content

Sound on collision with ground


yerzhik
 Share

Recommended Posts

My aim is to make a sound effect when a box hits the ground.

The problem is that box rotates and moves. Just doing box.intersectsMesh(ground, false); won't help in cases when box is moving and rotating on the ground, because it always will be intersecting the ground and only one sound effect will be made. But every hit of a side of box on the ground should make a new sound.

 

Are there any methods to identify the collision in my case? I have tried to use the:

box._boundingInfo.boundingBox.vectorsWorld, I thought its a points of 8 vectors of the box. 

Then by using them, I am always checking in 

scene.registerBeforeRender(function () {

...

}); function,

 

I check each of the 8 points if they intersect with ground object, using intersectsPoint  function for ground object.

If it intersects, then I push that point into a special array.

If next time it happens again, I do not treat it as a new intersection. 

If it doesn;t intersect, then I remove the point from array.

Each time new point is added into array a new collision sound is played.

 

But it didn't work because sound is always played even if box is not moving on the ground,

it means that intersection is always happening and then not happening (toggling).

 

Is there any similar project with collision detection?

 

Link to comment
Share on other sites

I have resolved the problem.

The issue was not in my code, but in the code of the babylon.js at least here:

babylon.boundingBox.js

 

BoundingBox.prototype.intersectsPoint = function (point) {            if (this.maximumWorld.x < point.x || this.minimumWorld.x > point.x)                return false;            if (this.maximumWorld.y < point.y || this.minimumWorld.y > point.y)                return false;            if (this.maximumWorld.z < point.z || this.minimumWorld.z > point.z)                return false;            return true;        };

Its not a good way to do it. I have rewrited the code in the following way (just for my application it works fine):

 

var intersectsPoint = function (point, boundingInfo, delta) {if (boundingInfo.maximumWorld.x - point.x < delta || delta > point.x - boundingInfo.minimumWorld.x)return false;if (boundingInfo.maximumWorld.y - point.y < delta || delta > point.y - boundingInfo.minimumWorld.y)return false;if (boundingInfo.maximumWorld.z - point.z < delta || delta > point.z - boundingInfo.minimumWorld.z)return false;return true;};

So I added delta as parameter, and instead of calling:

border.intersectsPoint(point);

I am calling:

intersectsPoint(point, border._boundingInfo.boundingBox, collisionDelta)

But I have done it just for boxex, but there are also function for boundingSphere.intersectsPoint

 

Hope that someone will fix the issue with too much sensitive (due to not using delta) intersectsPoint function.

 

Link to comment
Share on other sites

Could you elaborate more? I'm interesting in updating these functions with an epsilon value but I would like to be sure to understand why this is better to be less precise

I had a box moving and rotating on the floor. While moving I was checking if each of the 8 points of the box's corners is intersecting floor or not. If yes, the sound is played and the state of that point is remembered. If its still touching the ground the sound won't be played again, until the state of the point changes again (the point stops touching the ground). When box stops and not moving on the floor, the sound is still played. Because the intersectsPoint is returning true or false randomly. 

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