【发布时间】:2014-08-26 05:33:14
【问题描述】:
我在四个静态矩形对象内有一个动态圆形对象。考虑一个盒子里面的球,静态矩形对象模拟盒子的墙壁。对于圆形对象,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 的值,则会导致奇怪的碰撞响应。
有人知道如何让球物体在与静态物体碰撞后反弹回来吗?
【问题讨论】: