|
|
Hello,
I am new to farseer, i tried to create a Dynamic Body, with my spritesheet animation,
But the problem is, after that it loses it physicaly behaviour, i.e. it does not collide or bounce, it just bypass all the objects and goes down.
I am not sure how to apply spritesheet animation to a dynamic body.
Below is my code.
class AnimatingPhysicsObject
{
protected float width;
protected float height;
public Body body;
public Fixture fixture;
protected Texture2D texture;
protected Vector2 origin;
int elapsedTime;
// The time we display a frame until the next one
int frameTime;
// The number of frames that the animation contains
int frameCount;
int frameLines;
int currentLine = 0;
// The index of the current frame we are displaying
int currentFrame = 0;
// The color of the frame we will be displaying
Color color;
// The area of the image strip we want to display
Rectangle sourceRect = new Rectangle();
// The area where we want to display the image strip in the game
Rectangle destinationRect = new Rectangle();
// Width of a given frame
public int FrameWidth;
// Height of a given frame
public int FrameHeight;
// The state of the Animation
public bool Active;
// Determines if the animation will keep playing or deactivate after one run
public bool Looping;
// Width of a given frame
public AnimatingPhysicsObject(World world, Vector2 position, float width, float height, float mass, Texture2D texture,int frameWidth, int frameHeight, int frameCount,int frameLines,
int frametime, bool looping)
{
this.texture = texture;
this.origin = new Vector2(texture.Width / 2, texture.Height / 2);
this.width = width;
this.height = height;
this.FrameWidth = frameWidth;
this.FrameHeight = frameHeight;
this.frameCount = frameCount;
this.frameTime = frametime;
// Set the time to zero
elapsedTime = 0;
currentFrame = 0;
// bomb = Content.Load<Texture2D>("canon/bomb");
// Set the Animation to active by default
Active = true;
Looping = looping;
this.frameLines = frameLines;
SetUpPhysics(world, position, width, height, mass);
}
protected virtual void SetUpPhysics(World world, Vector2 position, float width, float height, float mass)
{
fixture = FixtureFactory.CreateRectangle(world, ConvertUnits.ToSimUnits(FrameWidth), ConvertUnits.ToSimUnits(FrameHeight), mass, ConvertUnits.ToSimUnits(position));
body = fixture.Body;
fixture.Body.BodyType = BodyType.Dynamic;
fixture.Restitution = 0.3f;
fixture.Friction = 0.5f;
}
public void Update(GameTime gameTime)
{
// Do not update the game if we are not active
if (Active == false)
{
currentFrame = 0;
currentLine = 0;
}
// fixture.Body.Position = Position;
// Update the elapsed time
elapsedTime += (int)gameTime.ElapsedGameTime.TotalMilliseconds;
// If the elapsed time is larger than the frame time
// we need to switch frames
if (elapsedTime > frameTime)
{
// Move to the next frame
currentFrame++;
// If the currentFrame is equal to frameCount reset currentFrame to zero
if (currentFrame == frameCount)
{
currentFrame = 0;
currentLine++;
if (currentLine == frameLines)
{
currentLine = 0;
// If we are not looping deactivate the animation
Active = false;
}
}
// Reset the elapsed time to zero
elapsedTime = 0;
}
// Grab the correct frame in the image strip by multiplying the currentFrame index by the frame width
sourceRect = new Rectangle(currentFrame * FrameWidth, currentLine * FrameHeight, FrameWidth, FrameHeight);
// Grab the correct frame in the image strip by multiplying the currentFrame index by the frame width
destinationRect = new Rectangle((int)ConvertUnits.ToDisplayUnits(body.Position.X) - (int)(FrameWidth) / 2,
(int)ConvertUnits.ToDisplayUnits(body.Position.Y) - (int)(FrameHeight) / 2,
(int)(FrameWidth),
(int)(FrameHeight));
}
public virtual void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, destinationRect, sourceRect, Color.White, body.Rotation, origin, SpriteEffects.None, 0f);
}
}
Please help, its urgent.
|
|
|
|
Could be an aligment issue between the sprite and the fixture. Enable the DebugView on top of your Draw to check. See the Samples for an example.
Also you do call World.Step() in your main logic and move the body by applying forces or changing the velocity and not teleporting it, right?
|
|
|
|
Yes i am doing World.Step() in my main logic update method.
right now i am not applying forces to the body , it is coming down due to gravity of the world.
i am not sure how to check it using DebugView
|
|
|
|
Do you have issues adding the DebugView to your game? It is quite easy - check the ScreenManager/PhysicsGameScreen.cs in the Samples. You can draw the debug data together with your own graphics code. It will show you what is actually present in the
physics world and what collides with what in the physics simulation.
|
|