Hello,
I also asked this in Omega's own forums. But it is very quiet there those days. And I got so much help here the last time and cannot find the problem myself and it has not much to do with Omega at least, perhaps somebody here can help me.
I wanted to have an own collision detection because I only need the collision detection to work on a part of each sprite, when moving sprites on the surface. So a player cannot walk 'through' a tree, but it can walk behind the top of the tree etc.
So I added to OmegaSprite Item a public RectCol: TRect;
This should be the area to make the collision check.
First thing is in each Sprite creation method I do it like this:
So this is the area for this sprite to collide.Rectcol.Top:=35;
Rectcol.left:=55;
Rectcol.bottom:=90;
Rectcol.right:=105;
Then in sprites move method:
So in the move method the collision detection is called, and when it collides, the sprite is set back to the location it had before. Xco and Yco are my own coordinates for the sprite in the world, X and Y are calculated from them.begin
lastxco:=xco;
lastyco:=yco;
...
move the sprite whereever
...
if commandoform.rect_collide(tsprite(self)) then begin
xco:=lastxco;
yco:=lastyco;
end;
//if a collision detection returns true set the sprite
//back to location it had before
end;
Here my collision detection:
This works very well in most cases, but just sometimes my man or monster can walk through a tree or another sprite. And it is sometimes hard to get away from another sprite when I am near to it, but not always.function TCommandoForm.rect_collide(SpriteSelf:TSprite): boolean;
var i: integer;
l,r,t,b: single;
begin
result:=false;
for i:=0 to xensprite.count-1 do begin
if (spriteself <> xensprite.Items[i]) and (xensprite.Items[i].name<>'Bullet') and
((max(spriteSelf.rectcol.left+spriteSelf.x,xenspri te.Items[i].Rectcol.left+xensprite.Items[i].x) < min(spriteSelf.Rectcol.right+spriteSelf.x, xensprite.Items[i].Rectcol.right+xensprite.Items[i].x)) and
(max(spriteSelf.Rectcol.top+spriteSelf.y, xensprite.Items[i].Rectcol.top+xensprite.Items[i].y) < min(spriteSelf.Rectcol.bottom+spriteSelf.y, xensprite.Items[i].Rectcol.bottom+xensprite.Items[i].y))) then begin
result:=true;
exit;
end;
end;
result:=false;
end
Do you have a better idea how to do it or a suggestion how to improve it or an idea why it is not working better?
Firle
Bookmarks