Hello everyone! This is my second time posting on this website, and so far I have very much appreciated the professional help its members have to offer. I am creating a custom physics engine for a game mod and am having some trouble dealing with collision detection and response. This might sound like a common thread topic, but in my case, some special circumstances are making the process more difficult.
First of all, the game has trouble dealing with a situation where, when a loop which is run every frame, contains a function which in turn contains its own loop. This usually causes the game to bog down or the entire game loop to be aborted. This happens even if the function only contains an empty loop iterating from 1 to 10. The game is much more tolerant towards nesting loops inside eachother when inside the same funciton.
Second, the game is written in a OOP-style scripting language to which there are no pre-existing libraries to include. This means i have to build everything from scratch - i currently have things like vector/quaternion libraries and a module for adding linked lists to classes. So before you suggest raycasting solutions, please explain how to best implement this in a cheap way, keeping in mind the loop issue.
Some background to my system:
Every frame, two separate loops are run - one for all rigid bodies, applying gravity, friction, velocities and so forward, another for collisions. There, I check first if the objects collide with eachother, and second if they collide with static objects in the game (such as walls, represented by AABBs). I do this by first doing a basic boundary check with an AABB or sphere, and I also plan to implement a SAT test for object-object collisions. If collision occurs, a class instance is created containing information about which two objects are colliding, and if it is a object to object collision, or object to static. Every frame I also loop through all these instances and cleaning out any doubles (two objects colliding will render two collision instances).
After this, it is time to push the objects apart, and this is what I find tricky. The way I define wether two objects have collided is to check if all axes intersect, but then how do i know on which axis to push them apart? Pushing them along the vector between the objects sound silly to me, because wether I push an object sidewards into a wall, the force should only push it outwards in the direction of the planes normal, and not stop the sidewards motion. How do I find which plane the object is colliding with?
NOTE: Something I just came up with while typing this, can I do it by calculating the normals of each surface of the box, and then do a dot product between them and the velocity vector of the object, picking the normal which has the lowest dot value?
Second thing is when an object is resting upon another object, like a soldier standing on a tank. I understand you are to apply some kind of constraint to bind the soldier to the tank, how does this work?
Thirdly, what is the best way to do collision check on fast moving objects, such as bullets? Is it possible to somehow check a line against a box without using loops, and to low expense?
As a side note I can mention that the requirements of precision in this engine are very low. I am doing all i can to simplify things while still having them look acceptably good. Any suggestions overall about how to structure a physics engine or collision detection are very welcome.