Hello,
my big problems right now are collision detection and pathfinding for my units in 3D.
Right now I am using one of Dans samples and enhanced it a bit.
My meshes are stored in a class that is based on a TList (here: Engine)
Bolmovable says if the mesh is allowed to move, nRadius if the XSize of the mesh/2.
Works good for some meshes, for others not.
I have some meshes that are not round (like houses) here only the 2D 'circles' are used.
Code:
function TForm1.boundcollide(spriteself: TDJXSprite): boolean;
var i: integer;
colSpr: TDJXSprite;
ColPt: TD3DXVector2;
tmpVec: TD3DXVector2;
newx,newz: single;
begin
//Collision with other sprites
//do not be alarmed by the complexity of the
//calculations, they are really simple
//once you take a closer look;)
for i:=0 to Engine.Count-1 do
begin
if (Engine.Sprites[i]<>spriteself) and
(distance_dbl(spriteself.x,spriteself.z,0,Engine.Sprites[i].X,Engine.Sprites[i].Z,0) < Innomsprite(spriteself).nRadius+Innomsprite(Engine.Sprites[i]).nRadius)
then
begin
colSpr:=Engine.Sprites[i];
ColPt:=D3DXVector2((spriteself.X+colSpr.X)/2,(spriteself.z+colSpr.z)/2);
tmpVec:=D3DXVector2(spriteself.X - colSpr.X,spriteself.z - colSpr.z);
D3DXVec2Normalize(tmpVec, tmpVec);
if innomsprite(spriteself).bolmovable then
begin
if innomsprite(ColSpr).bolmovable then
begin
newx:=ColPt.x + tmpVec.x * innomsprite(spriteself).nRadius;
newz:=ColPt.y + tmpVec.y * innomsprite(spriteself).nRadius;
if (spriteself.x<>newx) or (spriteself.z<>newz) then result:=true;
spriteself.x :=newx;
spriteself.z :=newz;
end
else
begin
spriteself.x := ColSpr.x + tmpVec.x * innomsprite(ColSpr).nRadius + tmpVec.x * innomsprite(spriteself).nRadius;
spriteself.z := ColSpr.z + tmpVec.y * innomsprite(ColSpr).nRadius + tmpVec.y * innomsprite(spriteself).nRadius;
end;
end;
if innomsprite(ColSpr).bolmovable then
begin
if innomsprite(spriteself).bolmovable then
begin
ColSpr.x := ColPt.x - tmpVec.x * innomsprite(ColSpr).nRadius;
ColSpr.z := ColPt.y - tmpVec.y * innomsprite(ColSpr).nRadius;
end
else
begin
ColSpr.x := spriteself.x - tmpVec.x * innomsprite(spriteself).nRadius - tmpVec.x * innomsprite(ColSpr).nRadius;
ColSpr.z := spriteself.z - tmpVec.y * innomsprite(spriteself).nRadius - tmpVec.y * innomsprite(ColSpr).nRadius;
end;
end;
end;
end;
end;
I guess to compare the meshes itself is too slow, so maybe if this collision returns true, it could check the meshes then or something? Or perhaps enhance it for rectangles also?
Does somebody have a good (and fast) solution for this matter?
Then pathfinding comes next.
Firle
Bookmarks