I have a collision question for in my game if anyone can help?
I want to stop the circle (bubble) when it hits a sloped tile (45 degrees) moving either up / down / left / right. I can do square tiles, but not sure how to do sloped:
Code:
|\ __
|_\ \ |
\|
___
| / /|
|/ /_|
Here is a screenshot (includes gravity vector when tilting world) which shows the sloped tiles as well as the square tiles:
This is my collision code for square tiles which I hope to add sloped tiles to:
Code:
var
Corners: SpriteCorners := new SpriteCorners;
method PoppedApp.GetBubbleCorners(aOfsX,aOfsY: Single): SpriteCorners;
begin
// calculate tile index for each object corner
Corners.Left := FLevel.getTileX(FBubble.pos.x - FBubble.radius + aOfsX);
Corners.Right := FLevel.getTileX(FBubble.pos.x + FBubble.radius - 0.001 + aOfsX);
Corners.Up := FLevel.getTileY(FBubble.pos.y - FBubble.radius + aOfsY);
Corners.Down := FLevel.getTileY(FBubble.pos.y + FBubble.radius - 0.001 + aOfsY);
// if true then there is a wall there
Corners.WallUL := FLevel.wallAt(Corners.Left ,Corners.Up);
Corners.WallUR := FLevel.wallAt(Corners.Right ,Corners.Up);
Corners.WallDL := FLevel.wallAt(Corners.Left ,Corners.Down);
Corners.WallDR := FLevel.wallAt(Corners.Right ,Corners.Down);
Result := Corners;
end;
method PoppedApp.MoveBubble(aTimeSlice: Single);
var
cnr: SpriteCorners;
begin
cnr := GetBubbleCorners(0,FBubble.vel.y*aTimeSlice);
if FBubble.vel.y < 0 then
// moving up
begin
if not cnr.WallUL and not cnr.WallUR then
// not obstructed so move normally
FBubble.pos.y := FBubble.pos.y + FBubble.vel.y * aTimeSlice
else
// hit tile so move to edge of tile and stop
begin
FBubble.vel.y := 0;
FBubble.pos.y := cnr.Up * cTileSize + cTileSize + FBubble.radius;
end;
end
else
if FBubble.vel.y > 0 then
// moving down
begin
if not cnr.WallDL and not cnr.WallDR then
// not obstructed so move normally
FBubble.pos.y := FBubble.pos.y + FBubble.vel.y * aTimeSlice
else
// hit tile so move to edge of tile and stop
begin
FBubble.vel.y := 0;
FBubble.pos.y := cnr.Down * cTileSize - FBubble.radius;
end;
end;
cnr := GetBubbleCorners(FBubble.vel.x*aTimeSlice,0);
if FBubble.vel.x < 0 then
// moving left
begin
if not cnr.WallUL and not cnr.WallDL then
// not obstructed so move normally
FBubble.pos.x := FBubble.pos.x + FBubble.vel.x * aTimeSlice
else
// hit tile so move to edge of tile and stop
begin
FBubble.vel.x := 0;
FBubble.pos.x := cnr.Left * cTileSize + cTileSize + FBubble.radius;
end;
end
else
if FBubble.vel.x > 0 then
// moving right
begin
if not cnr.WallUR and not cnr.WallDR then
// not obstructed so move normally
FBubble.pos.x := FBubble.pos.x + FBubble.vel.x * aTimeSlice
else
// hit tile so move to edge of tile and stop
begin
FBubble.vel.x := 0;
FBubble.pos.x := cnr.Right * cTileSize - FBubble.radius;
end;
end;
// apply gravity
FBubble.vel.x := FBubble.vel.x + FGravityX * aTimeSlice;
FBubble.vel.y := FBubble.vel.y + FGravityY * aTimeSlice;
// don't allow bubble to move faster than maximum velocity
if FBubble.vel.VecLen > cMaxVel then
begin
var m : Single := FBubble.vel.VecLen;
FBubble.vel.SetValue(FBubble.vel.x * cMaxVel / m,FBubble.vel.y * cMaxVel / m);
end;
end;
cheers,
Paul
Bookmarks