Hello everyone,
On my quest to find better (and more simple) ways to do collision I've stumbled upon this on a Asypre 2d tutorial, and quickly converted it to Andorra 2d ( did not knew before that I can get Sprite values from OnCollision ( example : TileLeft := Trunc(TPlatform(Sprite).X); )
// though I was wondering why it was written all over the Andorra 2d tutorials that the sprite engine is a very powerfull thing and that you are capable of writing games with it, given if you know how
Anyway it works, but not perfectly because I don't understand what these +4 -2 +5 etc are for... I've already changed parts of the original code which I was 100% sure in. The problem I'm having manifests itself by allowing my character to move inside tile if he's under it and he's head is just a bit higher then tile bottom, I can then move left and right in it.
I've opened the resource file that comes with the original project, the HERO pattern size is 32x48 , the blocks that he can do collision with are 32x32 (block1) and 64x64 (block2)
Part of the original code
Code:
TileLeft := Trunc(TTile(Sprite).X);
TileTop := Trunc(TTile(Sprite).Y);
TileRight := Trunc(TTile(Sprite).X) + TTile(Sprite).PatternWidth;
TileBottom := Trunc(TTile(Sprite).Y) + TTile(Sprite).PatternHeight;
if (TTile(Sprite).ImageName = 'Block1') or (TTile(Sprite).ImageName = 'Block2') then
My modified onMove code
Code:
procedure TBlurp.DoMove(TimeGap: Double);
begin
inherited;
Left := Round(X+2); // what is this +2 , same with +1 so it seems,no + not work
Top := Round(Y+4); // what is this ?
Right := Round(X + Image.Width);
Bottom := Round(Y + Image.Height);
// setting the camera to follow our HERO
if (X > 320) and (X < (40*TileW)-320-TileW) then
begin
// - 64 , because when loading the level my entire background is starting from 64 instead of 0
// looks better if there is no black line at the edge of the level...
camera_x:=(X*-1)+320-TileW;
end;
if (Y > 240) and (Y < (29*TileH)-240) then
begin
// 29 tiles down * 64 tile.height -240 half of the screen, only moving when at center of the screen
// well not really the center...but close anyway :)
camera_y:=(Y*-1)+240;
end;
if GetKeyState(VK_LEFT) < 0 then
begin
vx:=123;
x := x - vx * TimeGap;
end;
if GetKeyState(VK_RIGHT) < 0 then
begin
vx:=123;
x := x + vx * TimeGap;
end;
if GetKeyState(VK_UP) < 0 then
begin
vy:=123;
y := y - vy * TimeGap;
end;
if GetKeyState(VK_DOWN) < 0 then
begin
vy:=123;
y := y + vy * TimeGap;
end;
Collision;
end;
The oncollision
Code:
procedure TBlurp.DoCollision(Sprite:TSprite; var Done:boolean);
var
TileLeft, TileRight,
TileTop, TileBottom: Integer;
begin
if Sprite is TPlatform then
begin
TileLeft := Trunc(TPlatform(Sprite).X);
TileTop := Trunc(TPlatform(Sprite).Y);
TileRight := Trunc(TPlatform(Sprite).X) + TPlatform(Sprite).Image.Width;
TileBottom := Trunc(TPlatform(Sprite).Y) + TPlatform(Sprite).Image.Height;
// up
if GetKeyState(VK_UP) < 0 then
begin
if (Self.Top+5 > TileBottom) and
(Self.Right-4 > TileLeft) and
(Self.Left+3 < TileRight) then
Y := TileBottom;
end;
// down
if GetKeyState(VK_DOWN) < 0 then
begin
if (Self.Bottom -4 < TileTop ) and
(Self.Right -4 > TileLeft) and
(Self.Left +3 < TileRight) then
Y := TileTop-60;
end;
// left
if GetKeyState(VK_LEFT) < 0 then
begin
if (Self.Left+8 > TileRight) and
(Self.Top+5< TileBottom) and
(Self.Bottom-8>TileTop) then
X := TileRight;
end;
// right
if GetKeyState(VK_RIGHT) < 0 then
begin
if (Self.Right-8 < TileLeft )and
(Self.Top+5< TileBottom) and
(Self.Bottom-8>TileTop) then
X := TileLeft -30;
end;
end;
end;
Again I don't understand these +5 -8 etc... what is happening here ?
I've attached my source which is almos working correctly...
And the original material
Greetings
Robert
SpriteEngineRPGMap.zip
unSimple 0.1.zip
Bookmarks