PDA

View Full Version : Collision detection problems



MarkAshley
26-11-2004, 05:35 PM
Hi guys

I'm quite new to Delphix and am trying to write my first game - a Bomberman clone. I have successfully completed the "Pacman" collision detection tutorial and the collision detection worked. However, I can't get this to work in my own code. I have compared the equivalent parts of my code with the code from the tutorial and it all looks ok. I'd be really grateful if anybody could look over my code and help me find out what's wrong.

Thanks
Mark


TWhite = class(TImageSprite)
public
procedure DoMove(MoveCount: Integer); override;
procedure DoCollision(Sprite: TSprite; var Done: Boolean); override;
end;

TBrick = class(TImageSprite)
public
end;

var
White: TWhite;
bricks: array[1..150] of TBrick;
map: TStrings;

implementation

{$R *.dfm}

procedure setupmap();
var
i: integer;
begin
map:=TStringList.Create;
for i:=1 to 15 do map.Add('eeeeeeeeeeeeeee');
end;

procedure placebricks();
var
i,x,y,l: integer;
t: string;
begin
randomize();
for i:=1 to 150 do begin
l:=0;
while l=0 do begin
x:=random(15);
y:=random(15);

if map[y][x+1]='e' then begin
if x+y>1 then begin
t:=map[y];
t[x+1]:='b';
map[y]:=t;
l:=1;
end;
end;
end;

Bricks[i] := TBrick.Create(form1.DXSpriteEngine1.Engine);
Bricks[i].Image := Form1.DXImageList1.Items.Find('Brick');
Bricks[i].X := 25+x*50;
Bricks[i].Y := y*50;
Bricks[i].Height := 51;
Bricks[i].AnimCount := 1;
Bricks[i].AnimStart := 0;
Bricks[i].AnimLooped := True;
Bricks[i].AnimSpeed := 50/1000;
end;
end;

<MAP SETUP CODE REMOVED>

procedure TForm1.DXTimer1Timer(Sender: TObject; LagCount: Integer);
begin

<INPUT CAPTURE AND SPRITE MOVEMENT CODE REMOVED>

DXImageList1.Items.Find('Background').
Draw(DXDraw1.Surface,0,0,0);

DXSpriteEngine1.Move(2);
DXSpriteEngine1.Draw;
DXDraw1.Flip;

end;

procedure TWhite.DoMove(MoveCount: Integer);
begin
inherited;
PixelCheck := True;
Collision;
end;

procedure TWhite.DoCollision(Sprite: TSprite; var Done: Boolean);
begin
showMessage('bang');
end;

end.

Traveler
27-11-2004, 12:04 PM
Using the DelphiX collision detection procedure to check whether you've hit a wall is not the best way. Generally these games use a tilemap to check the position of, in this case, bomberman.

ie suppose your map looks like :
11111111111
1X000000X01
11010110101
1101B110001
11010111011
1100X000001
11111111111

where
1 = wall
0 = path
B = Bomberman
X = special bonus
First tile (top,left) at coordinates (0,0)=1
Each tile and bomberman itself is say 64x64 pixels big.

Now, you can check the coordinates of Bomberman in this map by dividing the x and y of bomberman by 64.


//tile record
type
TTile = record
image : TDirectDrawSurface;
wall : boolean;
bonus : boolean;
visible : boolean;
(..)
end;
(..)
var
Tilemap = array [0..20][0..20] of TTile;
(..)
if tilemap[bomberman.x div 64][bomberman.y div 64].wall = true then
//do something here, like stop moving or remove bonus etc


I realize, all this doesn't help you with the collisions problems, but it will give you a better start and less problems (at least I hope so).
If you need more information about tiles, check the tutorials at my website. (http://www.gameprogrammer.net)