# Collision between Blocks

Hey, I habe a little OpenGL project and I am curently Stuck in collision detection.
My funktion to Check if a collision between a Block and another block is going to happen within the next render call is working but I can’t figure a way out to prevent the camera from going inside the block. At the beginning, I tried to analyze DeltaPos. I used a function which gave me the main Direction. Then, I set the coordinate aufs the movement Vektor 0. If mainDir von DeltaPos x then movement = vec3(0,movement,y,movement.z). That worked, when you fly directly into the Block, but when you got are diagonal, the algorithm would set y coordinate 0, but we are still going into the x direction. So that waz, it isn’t working.

``````
procedure TForm1.MovementCalc;
var
BlockPos,OldPos,NewPos,DeltaPos,testmovement:TVector3f;
s1,s2,s3:string;
k:integer;
begin
OldPos:=CamPos;
MouseMovement;
if GetKeyState(VK_W) < 0 then movement := movement + camDir*deltaTime;
if GetKeyState(VK_S) < 0 then movement := movement - camDir*deltaTime;
if GetKeyState(VK_A) < 0 then movement := movement - Right*deltaTime;
if GetKeyState(VK_D) < 0 then movement := movement + Right*deltaTime;
if GetKeyState(VK_SPACE) < 0 then movement := movement + vec3(0,1,0)*deltaTime;
if GetKeyState(VK_SHIFT) < 0 then movement := movement - vec3(0,1,0)*deltaTime;
movement:=movement*movementspeed;
NewPos:=CamPos+movement;
DeltaPos:=NewPos-OldPos;
For ActiveBlock := 0 to ABlocklist.count-1 do
begin
BlockPos:=ABlockList[ActiveBlock].obj.GetPosVec;
if CheckCollision(NewPos,BlockPos) = true
then
begin
TestMovement:=vec3(0,Movement[1],Movement[2]);
OldPos:=CamPos;
NewPos:=CamPos+Testmovement;
DeltaPos:=NewPos-OldPos;
if CheckCollision(NewPos,BlockPos) = true
then
begin
TestMovement:=vec3(Movement[0],0,Movement[2]);
OldPos:=CamPos;
NewPos:=CamPos+Testmovement;
DeltaPos:=NewPos-OldPos;
if CheckCollision(NewPos,BlockPos) = true
then
begin
TestMovement:=vec3(Movement[0],Movement[1],0);
OldPos:=CamPos;
NewPos:=CamPos+Testmovement;
DeltaPos:=NewPos-OldPos;
Movement:=Testmovement;
end
else Movement:=Testmovement
end
else Movement:=Testmovement
end
end;

CamPos:=CamPos+movement;
str(camDir[0]:1:2,s1);
str(camDir[1]:1:2,s2);
str(camDir[2]:1:2,s3);
Caption:='ViewDir: '+s1+' '+s2+' '+s3+ '     '+ 'Facing: '+inttostr(GetFacing)+'     '+'Collision: '+ BoolToStr(CheckCollision(NewPos,BlockPos));

cam_inv.LookAt(camPos[0],camPos[1],camPos[2],camPos[0]+camDir[0],camPos[1]+camDir[1],camPos[2]+camDir[2],up[0],up[1],up[2]);
CheckOtherInputs;
end;

``````

The Part With getkeystate is for the definition of the movement vector, based on key inputs.

ABlocklist is a list with all the Blocks in my scene.

After that is the calculation of the movement, if a collision would happen in this render call.

FormCaption is not important, it is just for me to see certain variable values.

At first, it looked like it is working just fine, but if zou create more Blocks then just one and you are walking on the top from one block to another, there is like a like a obstacle.

Does anyone know why this happens? Is that a good solution or is there a better/Easier/more professional one?

I hope you can help me.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.