I have a box floating in the air above a plane with a certain degree, according to the picture.
I want the box to “land” below at the closest intersection point. How would I go about to do that? If I use a raycaster from each bottom corner of the box pointing down, I could get the Z coordinate and then set the box position there but if the box floats above the upper edge of the plane, it would not work as each intersection point would be “below” the edge.
Do you have any suggestions?
This may be helpful (lines 105 and further.)
Keep in mind - raycasting collisions is quite primitive by itself. To create a point-to-plane collision, simply take the centre of the bottom face of the cube, place the raycaster there (or a little above), then cast the ray downwards. This should be sufficient for most apps / games - you’ll get the point of intersection (as
point) and normal direction of the collision (as
face.normal) - which is more than enough to fake a net force acting on a body (ie. place the object at the intersection point, then rotate it to match the collision normal.)
If you want to raycast all 4 of the bottom vertices - you may want to consider writing / using a simple physics engine for the collisions - that would calculate per-vertex collision forces, net angular velocity etc.
Thanks for above, and the great example.
However, I would really like an “exact” way of doing it. Another example - Lets have big cube above a small cube and I want to put the larger cube on top of the smaller one. Using the center of bottom face and also using the 4 bottom vertices - it could still miss the bottom one. How does one do this?