sidd
sidd

Reputation: 669

aframe a-physics detect collision b/w 2 static bodies

Hi I am new to aframe and trying to do collision detection b/w 2 bodies which have the component of static-body attached with them.

I don't why the event collide is not being fired as its working fine b/w 1 dynamic and 1 static body but not working with 2 static bodies.

Kindly suggest a way to detect collision b/w 2 static-bodies using aphysics or if there is any other way let me know.

I am moving the static body using setInterval() and changing its position using setAttribute('position','x y z');

Thanks a ton in advance.

Upvotes: 0

Views: 462

Answers (1)

Piotr Adam Milewski
Piotr Adam Milewski

Reputation: 14645

From the a-frame physics documentation:

static-body: A fixed-position or animated object. Other objects may collide with static bodies, but static bodies themselves are unaffected by gravity and collisions.

Static bodies are unaffected by collisions.


If they can't be dynamic, I would suggest tracking their position + volume on tick and checking if they are colliding ( should be easy for primitive objects ).


If You have two spheres (s(x1,y1,z1) and s2(x2,y2,z2)), You can add a master component in the scene checking their distance from each other using a simple distance between 2 points formula:

 tick:function(){
   if(Math.sqrt(Math.pow((x1-x2),2) + Math.pow((y1-y2),2) + Math.pow((z1-z2),2)))<(sRadius+s2Radius)){
   //do stuff when spheres collide
   }
 }

From what i see, three.js has its own intersect methods, You can find them here ( methods: intersect(), intersectsBox(),etc...). To access the three.js object, get the el.object3D reference.

The intersection object is defined like this:

{ distance, point, face, faceIndex, indices, object }
distance – distance between the origin of the ray and the intersection
point – point of intersection, in world coordinates
face – intersected face
faceIndex – index of the intersected face
indices – indices of vertices comprising the intersected face
object – the intersected object

as explained in the three.js documentation.

For intersection of other primitives, You'll need to research algorithms for intersections, as the algorithms i have in my mind are highly inefficient.

Upvotes: 1

Related Questions