C# 如何修复平台碰撞检测?
我正在开发一款游戏,它允许我的2D精灵在平台上跳跃,但当它降落在平台上时,我的2D精灵并不完全在平台的顶部,有时超过或等于平台的位置C# 如何修复平台碰撞检测?,c#,xna,C#,Xna,我正在开发一款游戏,它允许我的2D精灵在平台上跳跃,但当它降落在平台上时,我的2D精灵并不完全在平台的顶部,有时超过或等于平台的位置 //here is my code: Texture2D jumper; Texture2D tile; Vector2 position, velocity; Rectangle top; Rectangle tile_rectangle; Rectangle jumper_rectangle; KeyboardState ks; bool jump; /
//here is my code:
Texture2D jumper;
Texture2D tile;
Vector2 position, velocity;
Rectangle top;
Rectangle tile_rectangle;
Rectangle jumper_rectangle;
KeyboardState ks;
bool jump;
//here is my Load Content:
protected override void LoadContent()
{
jump = true;
position.X = 10;
position.Y = 10;
jumper = Content.Load<Texture2D>("character");
tile = Content.Load<Texture2D>("tile");
tile_rectangle = new Rectangle(300, 350, tile.Width, tile.Height);
top = new Rectangle(tile_rectangle.X, tile_rectangle.Y - 16, tile_rectangle.Width, 10);
}
//Here is my update:
protected override void Update(GameTime gameTime)
{
ks = Keyboard.GetState();
position += velocity;
float i = 1;
if (ks.IsKeyDown(Keys.Up) && jump == false)
{
position.Y -= 10f;
velocity.Y = -25f;
jump = true;
}
if (jump == true)
{
velocity.Y += 2f * i;
}
if (position.Y > 400)
{
jump = false;
}
if (jump == false)
{
velocity.Y = 0f;
}
BoundingBox();
if (ks.IsKeyDown(Keys.Right))
{
position.X += 5f;
velocity.X = +0.05f;
if (jumper_rectangle.Left > tile_rectangle.Right)
{
jump = true;
}
}
if (ks.IsKeyDown(Keys.Left))
{
position.X -= 5f;
velocity.X = -0.05f;
if (jumper_rectangle.Right < tile_rectangle.Left)
{
jump = true;
}
}
jumper_rectangle = new Rectangle((int)position.X, (int)position.Y, jumper.Width, jumper.Height);
BoundingBox();
base.Update(gameTime);
}
//here is my draw:
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(jumper, jumper_rectangle, Color.White);
spriteBatch.Draw(tile, tile_rectangle, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
//here is my method on landing:
public void BoundingBox()
{
if (jumper_rectangle.Intersects(top))
{
if (jumper_rectangle.Bottom > top.Top)
{
position.Y--;
jump = false;
}
}
}
//这是我的代码:
纹理2D跳线;
纹理2D瓷砖;
矢量2位置、速度;
矩形顶部;
矩形瓦片(u矩形),;
矩形或U形矩形;
键盘状态;
跳远;
//以下是我的加载内容:
受保护的覆盖void LoadContent()
{
跳跃=真;
位置X=10;
位置Y=10;
跳线=内容加载(“字符”);
瓷砖=内容。荷载(“瓷砖”);
tile_rectangle=新矩形(300350,tile.Width,tile.Height);
顶部=新矩形(tile\u Rectangle.X,tile\u Rectangle.Y-16,tile\u Rectangle.Width,10);
}
//以下是我的更新:
受保护覆盖无效更新(游戏时间游戏时间)
{
ks=Keyboard.GetState();
位置+=速度;
浮点数i=1;
if(ks.IsKeyDown(Keys.Up)&&jump==false)
{
位置Y-=10f;
速度Y=-25f;
跳跃=真;
}
如果(跳转==真)
{
速度Y+=2f*i;
}
如果(位置Y>400)
{
跳跃=假;
}
如果(跳转==false)
{
速度Y=0f;
}
边界框();
if(ks.IsKeyDown(key.Right))
{
位置X+=5f;
速度X=+0.05f;
如果(跳线\u矩形.左>平铺\u矩形.右)
{
跳跃=真;
}
}
if(ks.IsKeyDown(Keys.Left))
{
位置X-=5f;
速度X=-0.05f;
if(跳线\u矩形。右<平铺\u矩形。左)
{
跳跃=真;
}
}
跳线_矩形=新矩形((int)位置.X,(int)位置.Y,跳线.宽度,跳线.高度);
边界框();
更新(游戏时间);
}
//这是我的画:
受保护覆盖无效绘制(游戏时间游戏时间)
{
图形设备。清晰(颜色:矢车菊蓝);
spriteBatch.Begin();
spriteBatch.Draw(跳线,跳线为矩形,颜色为白色);
spriteBatch.Draw(平铺、平铺矩形、彩色、白色);
spriteBatch.End();
基础。抽签(游戏时间);
}
//以下是我的着陆方法:
公共无效边界框()
{
if(跳线与矩形相交(顶部))
{
如果(跳线\u矩形底部>顶部顶部)
{
位置Y--;
跳跃=假;
}
}
}
我哪里出错了,还是有别的办法 if(jumper\u rectangle.Bottom>=top.top){position.Y=top.top-1;jump=false;}
还有为什么要检查第二个if语句是否相交呢?if(jumper\u rectangle.Bottom>=top.top){position.Y=top.top-1;jump=false;}还有为什么要检查第二条if语句中是否有相交。