Everything I've done till now kinda works as intended, but with some little bugs which I can't think of a solution to fix them.
First, let me show you this block of code where I apply my gravity and a little function for collision detection:
// Apply gravity
if (isInAir == true)
{
position.Y += velocity.Y * curTime;
velocity.Y += gravity;
}
else
{
position.Y = tilePosY - texture.Height;
velocity.Y = 0f;
}
if (position.Y > 400) position.Y = -16;
// Check for tile collision
CheckCollisions();
Obviously, these are located in a Player class, in the Update method. Now, follows the CheckCollisions() function which is separate just to keep the code in the Update cleaner:
public void CheckCollisions()
{
foreach (Tile t in Game1.tiles)
{
if (recBot.Intersects(t.rectangle))
{
colBot = true;
tilePosY = t.position.Y;
}
if (recRight.Intersects(t.rectangle))
{
colRight = true;
tilePosX = t.position.X - texture.Width;
}
if (recLeft.Intersects(t.rectangle))
{
colLeft = true;
tilePosX = t.position.X + t.texture.Width;
}
}
}
The Update() method looks something like this, with some little modifications to shorten it:
public void Update(GameTime gameTime)
{
float curTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
position += velocity * curTime;
// The movement
PlayerMovement();
//Create the rectangles - left and right rectangles removed
recBot = new Rectangle((int)position.X, (int)position.Y + texture.Height, texture.Width, 4);
// Set the collisions to false
CollisionFalse();
// Apply gravity - the first code block I showed you
// Check for tile collision
CheckCollisions();
// Check if the character is in air based on collision
CheckInAir();
}
Now the problem is, when I fall from a platform to the bottom-most ground, the sprite gets stuck and it's looking like it's going up and down 1 px in an infinite loop. You can download the source and run a quick test to see what I mean. It seems that the velocity on Y axis is too high, but dunno how to fix it to really stop when collision is detected.
Download the source code (my server, direct download)
Looking forward to see some tips from experts!

New Topic/Question
Reply


MultiQuote




|