1
Graphics / Re: Collision of two sprites is very weird
« on: April 27, 2019, 11:15:45 am »You didn't show how you draw your objects, but I would guess you're splitting up your player movement and collision response into separate frames, causing your object to alternate between collision and non-collision each frame. Basically, your logic probably flows something like:
1. Move object
2. Draw frame
3. If object is colliding, move object back.
4. Draw frame
5. Repeat
What you probably want is something more like:
1. Move object
2. If object is colliding, move object back.
3. Draw frame
4. Repeat
By the way, now might be a good time to learn how to use a debugger if you haven't already . These types of problems are usually pretty easy to spot when using one
Thanks again!! Now it's working. For others who maybe need help: I've changed the collision checker in my Update Method:
pos += vel * dt;
sprite.Position = pos;
bool IsCollision = collision();
Program.collisionText.DisplayedString = "Collision: " + IsCollision.ToString();
if (IsCollision)
{
sprite.Position = oldPos;
pos = sprite.Position;
}
else
{
oldPos = sprite.Position;
}
sprite.Position = pos;
bool IsCollision = collision();
Program.collisionText.DisplayedString = "Collision: " + IsCollision.ToString();
if (IsCollision)
{
sprite.Position = oldPos;
pos = sprite.Position;
}
else
{
oldPos = sprite.Position;
}