Given a spherical object A is colliding with object B, how do i find the contact point on object A?
Amnesia has no way, sadly, to determine the position of entities or contact points using available functions. Filling the entire map with script areas would solve the problem, but for just a 5x5x5 room, with enough script areas to accurately determine a box and a ball colliding, you'd be looking at 8,000 entities - then for
each of these areas you would have to check for collisions between the two objects to determine a set of points, This is far too much work to do without lag, let alone in a 200x200x20 map!
However, given that we know the two objects
must be close to each-other, we can reduce that obscene amount of areas to ~100 static entities, which exist for under a frame, and only a few of which are ever even in the game at the same time!
The solution: As soon as a collision occurs, create a large box around one of the colliding objects (in this mod it is the katamari, and is done using the AddAttachedPropToProp function). If there is a collision with the big box and both entities, we split the big box into 8 smaller boxes. We then do that same process for all 8 smaller boxes, and keep going until the boxes are really small. As soon as we have determined a collision for a box, we can destroy that box (RemoveAttachedPropFromProp).
This is like an
octree spatial partitioning technique, except we don't bother saving the tree, as it will be useless after we get the contact points. We keep dividing down and hone in on all the boxes that collide with both objects. We get a head-start, too, as we actually start at the centre of our spherical object.
At the smallest box size, we may have multiple boxes that collide with both entities! We want one contact point, not a set! Thankfully, because we created each box this frame, we know the position of each one relative to our first entity (the katamari). All we do is calculate the (square) distance from the katamari for each small box - we then only keep the position that was furthest away - as this will be the point at which the main object (katamari)
first touched the other object. This is all the information we need to get where to attach the object to on the katamari.
This is just a quick overview, the code this time around is much better organised and commented than with tetris, so when the mod is released and you want to see exactly how it was done, then it shouldn't be too hard to follow
.