Sunday, October 18, 2015

xna - Collision detection problem in monogame C#


    protected virtual void CollisionDetection()
{
if (CanGoRight() == false) pos.X -= velocity;
}


protected bool IsTouchingLeft(Basic2d asset)
{
return pos.X + dims.X / 2 > asset.pos.X &&
pos.X - dims.X / 2 < asset.pos.X &&
pos.Y + dims.Y / 2 < asset.pos.Y + dims.Y &&
pos.Y - dims.Y / 2 > asset.pos.Y;
}

protected bool CanGoRight()

{
foreach (Basic2d asset in Globals.GameAssets)
if (IsTouchingLeft(asset) == true) return false;
return true;
}
}

That's my code(just starting) for collision detection. All of this code is in Character class which has also methods for moving and updating and displaying character.


I struggle with collision detection. I have all my assets(blocks) in the list of objects which is in the class named Globals. Class character is a subclass of a Basic2d class(this is class for all bricks assets).


So I check if a character is overlapping with any of bricks from the list. Criteria for checking left side overlapping is in the IsTouchingLeft method and it doesn't work. I cannot see what is wrong with this code, but every time I change sth it works differently but not well.



What should I change?


And the object of Character class has an origin point in in the center of the texture, but the object of Basic2d class has origin point in the bottom left corner.




No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...