I have a dynamic circular object bounded inside four static rectangle objects. Consider a ball inside a box with the static rectangle objects simulating the walls of the box . IgnoreGravity is set to true for circular object. I want the circular object to hit and bounce back from the walls. However, when the circular object collides with the wall, it stucks to it and moves along the wall instead of bouncing back.
Following is the code I am using:
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;
}
....
}
After a little digging around I found out that the VelocityConstraint
was causing the collision to be treated as inelastic. However, if I reduce the value of VelocityConstraint
then it results in weird collision response.
Does anybody know how to get the ball object bounce back after colliding with the static objects?