|
发表于 2004-6-15 09:37:00
|
显示全部楼层
Re:请教人物在地图中移动碰壁的问题!
我写的一段delphi的,通过检查x/y 是否在一个rect 范围内实现的,另外,分别检测物体的四个角的碰撞情况
[a]
[Sp]
[c] [d]
//分别传入a..b的x/y/width/height 和sprite 的x/y/width/height 和方向
//判断四个方向为Up = a .. b down=c..d left=a...c right=b..d
function CheckSpriteRect(x1, y1, w1, h1, x2, y2, w2, h2, px, py, pw, ph, dir:
Integer): TCheckResult;
var
r1, r2 : TRect;
p1, p2 : TPoint;
m1, m2 : integer;
cb : boolean;
begin
SetRect(r1, x1, y1, x1 + w1, y1 + h1); //第一个
SetRect(r2, x2, y2, x2 + w2, y2 + h2); //第二个
m1 := 0;
m2 := 0;
case dir of
0: //up
begin
p1.X := px;
p1.Y := py;
p2.X := px + pw;
p2.Y := py;
m1 := w1 div 3;
m2 := w2 div 3;
end;
1: //down
begin
p1.X := px;
p1.Y := py + ph;
p2.X := px + pw;
p2.Y := py + ph;
m1 := w1 div 3;
m2 := w2 div 3;
end;
2: //left
begin
p1.X := px;
p1.Y := py;
p2.X := px;
p2.Y := py + ph;
m1 := h1 div 3;
m2 := h2 div 3;
end;
3: //right
begin
p1.x := px + pw;
p1.Y := py;
p2.x := px + pw;
p2.Y := py + ph;
m1 := h1 div 3;
m2 := h2 div 3;
end;
end;
cb := PtInRect(r1, p1) or PtInRect(r2, p2);
result.Stop := cb;
if result.Stop then
begin
if PtInRect(R1, p1) then
begin
result.Id := 1;
case Dir of
0, 1: //up;
if (R1.Right - p1.X) > m1 then
result.Id := 0;
2, 3:
if (r1.Bottom - p1.Y) > m1 then
result.Id := 0;
end;
end;
if PtInRect(R2, p2) then
begin
result.Id := 2;
case Dir of
0, 1:
if (p2.X - r2.Left) > m2 then
result.Id := 0;
2, 3:
if (p2.Y - r2.Top) > m2 then
result.Id := 0;
end;
end;
end
else
result.Id := -1;
end;
//返回类型为
type
TCheckResult = record
Stop: boolean; //是否停止
Id: integer; //0表示不调整坐标,1/2分别调整++/--
end;
//有相交的地方进行坐标调整,也就是人物贴与墙面时刻进行滑动的效果(C伪码)
if(result.id=1&&isLeft||isRight)Y++;
if(result.id=2&&isLeft||isRight)Y--;
if(result.id=1&&isUp||isDown)X++;
if(result.id=2&&isUp||isDown)X--;
|
|