gpt4 book ai didi

c# - Farseer:对象不会从静态对象弹回

转载 作者:太空宇宙 更新时间:2023-11-03 16:03:32 25 4
gpt4 key购买 nike

我有一个动态圆形对象,它被限制在四个静态矩形对象内。考虑一个盒子内的球,其中静态矩形对象模拟盒子的壁。对于圆形对象,IgnoreGravity 设置为 true。我希望圆形物体撞击墙壁并从墙壁反弹回来。然而,当圆形物体与墙壁碰撞时,它会粘在墙上并沿着墙壁移动而不是弹回。

以下是我使用的代码:

Body CircleBody;

const int CircleRadiusInPixels = 20;
const int CircleCentreXInPixels = 100;
const int CircleCentreYInPixels = 100;
Texture2D CircleTexture;

Body[] RectangleBody;
Texture2D RectangleTexture;

struct RectagleProperties
{
public int WidthInPixels;
public int HeightInPixels;
public int CenterXPositionInPixels;
public int CenterYPositionInPixels;

public RectagleProperties(int Width, int Height, int X, int Y)
{
WidthInPixels = Width;
HeightInPixels = Height;
CenterXPositionInPixels = X;
CenterYPositionInPixels = Y;
}

};

RectagleProperties[] rectangleProperties;

World world;

const float PixelsPerMeter = 128.0f;
float GetMetres(int Pixels)
{
return (float)(Pixels / PixelsPerMeter);
}

int GetPixels(float Metres)
{
return (int)(Metres * PixelsPerMeter);
}



protected override void LoadContent()
{
...
RectangleTexture = Content.Load<Texture2D>("Sprites/SquareBrick");
CircleTexture = Content.Load<Texture2D>("Sprites/Circle");

world = new World(new Vector2(0, 9.8f));

CircleBody = BodyFactory.CreateCircle(world, GetMetres(CircleRadiusInPixels),
1,
new Vector2(
GetMetres(CircleCentreXInPixels),
GetMetres(CircleCentreYInPixels)));
CircleBody.BodyType = BodyType.Dynamic;
CircleBody.IgnoreGravity = true;
CircleBody.LinearVelocity = new Vector2(1, 1);

rectangleProperties = new RectagleProperties[4];
rectangleProperties[0] = new RectagleProperties(800, 20, 400, 10);
rectangleProperties[1] = new RectagleProperties(800, 20, 400, 460);
rectangleProperties[2] = new RectagleProperties(20, 480, 10, 240);
rectangleProperties[3] = new RectagleProperties(20, 480, 790, 240);

RectangleBody = new Body[4];
for (int i = 0; i < rectangleProperties.Length; i++)
{
RectangleBody[i] = BodyFactory.CreateRectangle(world,
GetMetres(rectangleProperties[i].WidthInPixels),
GetMetres(rectangleProperties[i].HeightInPixels),
0.5f,
new Vector2(GetMetres(rectangleProperties[i].CenterXPositionInPixels),
GetMetres(rectangleProperties[i].CenterYPositionInPixels)));
RectangleBody[i].BodyType = BodyType.Static;

}
....
}

经过一番深入研究后,我发现 VelocityConstraint 导致碰撞被视为无弹性。但是,如果我减小 VelocityConstraint 的值,则会导致奇怪的碰撞响应。

有谁知道如何让球体在与静态物体碰撞后反弹回来?

最佳答案

由于您没有发布问题可能所在的更新方法的内容,我将做出一个合格的猜测。

这似乎与一个非常普遍的问题有关:

假设圆形物体有一定的速度。如果发生碰撞,您可以反转墙轴上的速度,并且速度会因重力或其他因素而降低一点。

现在您应用新的速度来解决碰撞,但由于速度降低,它不足以解决碰撞,这意味着该对象在下一次更新时仍会与墙壁发生碰撞。

这将导致速度反复反转,因为物体永远无法获得足够的速度来再次摆脱碰撞。

这个问题的解决方案是在继续其他代码之前,最好在最短的轴上移动物体使其脱离碰撞(查看“分离轴定理”)。

关于c# - Farseer:对象不会从静态对象弹回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20366159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com