gamelets

Issues


Collision Issues

Checking for an Intersect

One way to check in BabylonJS whether two meshes are colliding is to use the intersectsMesh method. So for Fig 1

sphere.intersectsMesh(wall)

would give the result true

Fig 1
     Fig 1

You may think that where the sphere in Fig 2 is travelling towards the wall that you could use

if(sphere.intersectsMesh(wall)) {
    //bounce sphere off wall
}
else {
    //move sphere left
}

Fig 2
     Fig 2

However consider the sphere as having unit diameter and is moving towards the ball with a speed of 5 units per frame. The check for collision has to be considered for each frame. The first frame is as in Fig 3 and the next frame as in Fig 4

Fig 3
     Fig 3

The sphere will have gone past the wall and is not intersecting it and so no collision will be detected

Fig 4
     Fig 4

Size and Speed Matter.

Consider a sphere of radius r travelling towards a wall with speed v, at a position just out of contact with the wall. To be able definitely to use of the intersectsMesh method the sphere must be in contact with the wall during the next frame. The maximum distance the sphere can travel between frames must be less than twice the radius, as in Fig 5.

Fig 5
     Fig 5

So for the sphere to start in any random position and travel directly towards the wall and for an intersection with the wall definitely to take place during a frame v <= 2r

Now consider two spheres travelling towards each other, each sphere having a speed v. The relative velocity is 2v. Take one sphere as stationary and the other moving with speed v at a position just out of contact with each other. To be able to use the intersectsMesh method the spheres must be in contact with each other during the next frame. The maximum distance the moving sphere can travel between frames must be less than four times the radius, as in Fig 6.

Fig 6
     Fig 6

So for two spheres to start in any random positions and travel directly towards each other and for an intersection with the wall definitely to take place during a frame 2v <= 4r, which is again v <= 2r.

To generalise , should one sphere have radius r1 and speed v1 and the other radius r2 and speed v2 intersections from random positions will definitely take place
when v1 + v2 < 2(r1 + r2)

What is the situation when v > 2r?

Consider that the front of the sphere is a distance d from the wall in a frame, F, and in the next frame, N, the sphere intersects the wall or has moved passed it, then 0 <= d <= v. If d > v then d - v > 0 and so in the next frame the sphere is still a distance d - v > 0 from the wall. In fact when the sphere is any distance p away from the wall, p > 0, then for frame F, d = p modulo v. Fig 7 shows that for the sphere to intersect the wall v - 2r < d V v.

Fig 7
     Fig 7

The sphere just touches the wall when d = 0 and so for a display where the sphere does not penetrate the wall p modulo v is 0.

Expressing this in quotient and remainder form p = qv, q >= 0

Since p is the distance from the front of the wall the centre of the sphere will be at qv + r.

Playground Example

In the following playground the three spheres are positioned so that for sphere1, d < v - r and so will pass through the wall; for sphere2 d = 0 and for sphere3 v - 2r < d < v. The radius and speed for each sphere have been set at the same values but can be changed easily should you wish to. Sphere1 is at the top, sphere2 the middle and sphere3 at the bottom.

Playground Example -


Summary

By carefully controlling the positions of the spheres with relationship to their velocity it is possible to simulate collisions between them so that they rebound when they touch.

The next section considers unrestriced positioning.

Moving Too Fast To Intersect