你好我是XNA的新手,并试图开发一个游戏原型,其中角色使用鼠标点击从一个位置移动到另一个位置。矩形中包含的XNA Vector2路径
我有一个Rectangle表示当前位置。我使用播放器鼠标输入将目标位置作为Vector2。我通过Vector2减法从源向目标提取方向矢量。
//the cursor's coordinates should be the center of the target position
float x = mouseState.X - this.position.Width/2;
float y = mouseState.Y - this.position.Height/2;
Vector2 targetVector = new Vector2(x, y);
Vector2 dir = (targetVector - this.Center); //vector from source center to target
//center
我代表世界使用瓷砖地图,每个单元格是32x32像素。
int tileMap[,];
我想要做的是检查上面的方向矢量是否通过地图上的任何蓝色瓷砖。地图上的蓝色小方块等于1。
我不知道如何做到这一点。我想过使用线性直线方程和三角函数公式,但我发现很难实现。我已经尝试了使矢量正常化并乘以32以获得沿着矢量路径的32个像素长度的间隔,但它似乎不起作用。任何人都可以告诉我它是否有任何问题,或者解决这个问题的另一种方法?由于
//collision with blue wall. Returns point of impact
private bool CheckCollisionWithBlue(Vector2 dir)
{
int num = Worldmap.size; //32
int i = 0;
int intervals = (int)(dir.Length()/num + 1); //the number of 32-pixel length
//inervals on the vector, with an edge
Vector2 unit = Vector2.Normalize(dir) * num; //a vector of length 32 in the same
//direction as dir.
Vector2 v = unit;
while (i <= intervals & false)
{
int x = (int)(v.X/num);
int y = (int)(v.Y/num);
int type = Worldmap.getType(y, x);
if (type == 1) //blue tile
{
return true;
}
else
{
i++;
v = unit * i;
}
}
return false;
}