我正在使用 XNA 的 monogame 实现为 Windows 商店制作游戏,其中,我正在应用轻弹手势在屏幕上移动对象。
这是我在Update 方法中编写的用于轻弹和更新对象的位置和速度的代码
while (TouchPanel.IsGestureAvailable)
{
GestureSample gesture = TouchPanel.ReadGesture();
if (gesture.GestureType == GestureType.Flick)
velocity += gesture.Delta;
if (gesture.GestureType == GestureType.Hold)
position = gesture.Position;
}
if (velocity != Vector2.Zero)
{
float elapsedSeconds = (float)gameTime.ElapsedGameTime.TotalSeconds;
position += velocity * elapsedSeconds;
float newMagnitude = velocity.Length() - DECELERATION * elapsedSeconds;
velocity.Normalize();
velocity *= Math.Max(0, newMagnitude);
}
UpdateSprite(gameTime, ref position, ref velocity);
void UpdateSprite(GameTime gameTime, ref Vector2 spritePosition, ref Vector2 spriteSpeed)
{
// Move the sprite by speed, scaled by elapsed time.
spritePosition += spriteSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
int MaxX = GraphicsDevice.Viewport.Width - cat.Width;
int MinX = 0;
int MaxY = 500 - cat.Height;
int MinY = 0;
// Check for bounce.
if (spritePosition.X > MaxX - 10)
{
if (spritePosition.Y <= 200 || spritePosition.Y >= 300)
{
spriteSpeed.X *= -1;
spritePosition.X = MaxX;
}
else
{
PositionCount = 0;
}
}
else if (spritePosition.X < MinX + 10)
{
if (spritePosition.Y <= 200 || spritePosition.Y >= 300)
{
spriteSpeed.X *= -1;
spritePosition.X = MinX;
}
else
{
PositionCount = 0;
}
}
if (spritePosition.Y > MaxY)
{
spriteSpeed.Y *= -1;
spritePosition.Y = MaxY;
}
else if (spritePosition.Y < MinY)
{
spriteSpeed.Y *= -1;
spritePosition.Y = MinY;
}
}
对象的轻弹与此代码完美配合。但我正在寻找的是,当用户轻弹对象时,如果轻弹对象与任何其他对象发生碰撞,那么第二个对象也应该移动,就像轻弹撞针与它们碰撞时 carom 硬币的移动方式一样。
我曾尝试为碰撞和移动添加逻辑但徒劳无功。
我们将不胜感激任何形式的帮助。
非常感谢。
最佳答案
使用Farseer Physics Engine .它是基于 Box2D 的流行 C# 物理引擎,并且与 MonoGame 兼容。它有很多代码示例和不错的文档。
关于c# - XNA 中两个纹理之间的碰撞检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20989246/