Hi!
I'm currently working on a platform 2D Engine. What I've accomplished so far is the ability to run around with my character on the map. It's a tilebased engine and the characters is represented by simple rectangles for collission detection against the tiles (no pixel-perfect collission). I've got a Update()-method for the character which looks like this:
void CPhysObject::Update(float fDT)
{
// Integrate
vVelocity = vVelocity + vAcceleration*fDT;
Main(); // Calls LUA-function Main for this character, allowing it to change speeds
vVelocity = vVelocity*fDT; // Scale the velocity vector for integration to position
HandleWorldCollission();
// Integrate
vPosition = vPosition + vVelocity;
vVelocity = vVelocity/fDT;
vNewPosition = vPosition;
}
The Main()-method calls a LUA-function "main" in the character .lua-file. The lua-file may then change velocities (for example; set the velocity.y-component to -5 if the user press space bar in order to make a jump). After the Main() has been called I have to scale the velocity with fDT, otherwise "HandleWorldCollission()" will not be able to predict the next position. The HandleWorldCollission() method starts with the characters' current position and moves in the velocity direction with small steps until it reaches (vPosition+vVelocity). In each small step collission against the world is checked and if a collission is found, the velocities are changed to "valid" values.
This is working really great actually, I'm able to set the game speed by changing fDT and regardless of which fDT I have the characters will have the same jump length. I also scale fDT with the FPS in order to make the game go equally fast on all computers. Now, the problem is that I can't figure out how to implement the Character-to-character collission response code. This is my idea of how it needs to be implemented:
* Find the character with the greatest velocity vector length
* Make "rays" with the character velocities divided by this length
* Loop from the characters start positions to the end positions using the rays. Detect any collissions and call a CollissionAccured()-function in the characters LUA-script to make the LUA-programmer decide what to do for response.
This feels rather complex and also I guess I need to check collission with the world yet another time again (!) since the velocities are changed due to character-collission-response. Also, when the lua-function CollissionAccured() is called, the velocities needs to be scaled down again (or the game wouldn't go equally fast on all computers).
Is this really the "right" way to do it? It just feels very very complex to move all the characters in small steps at the same time and also check collissions with the world 2 times.
Thanks in advance!
[edit: How do you make the source code syntax highlighted? I tried
but it didn't work..]