i'm working on a small RPG game, and what i currently have is:
*Character with animation and basic collision; *A 32x32 box which i test the collision on;
And thats all for now..xD
My character is 32xPixels wide, and 48xPixels tall, 32x38.
And i have Two rectangles, playerRectangle
(for collision and movement);
And rectangleAnimation
, for the character animation.
Well, my question is, how do i make a collision rectangle that only covers half of the character? Right now my rectangle is 32x48, so how do i make it 32x24, WITHOUT cropping the image?
It gives the illusion of being infront of something, for example a rock, or a tree.
I tried to make a seperate sprite, which is 32x24 and made a rectangle of it and drew two sprites in my player class, but that didn't work...
And my last question, should i make a seperate class, like CollisionHandler.cs
for all my solid things, like walls, and boxes?
Here's my movement code for the player(without attempting to make a diffrent sized rectangle):
if (keyState.IsKeyDown(Keys.Left))
{
playerRectangle.X -= playerSpeed;
movingLeft = true;
movingRight = false;
}
else if (keyState.IsKeyDown(Keys.Right))
{
playerRectangle.X += playerSpeed;
movingRight = true;
movingLeft = false;
}
if (keyState.IsKeyDown(Keys.Up))
{
playerRectangle.Y -= playerSpeed;
movingUp = true;
movingDown = false;
}
else if (keyState.IsKeyDown(Keys.Down))
{
playerRectangle.Y += playerSpeed;
movingDown = true;
movingUp = false;
}
(I'm sorry that the code looks messy, i'm new to this site and i don't know how to structure it :/)
And my collision check(Which is in Game1.cs):
if(isColliding())
{
if (player.movingRight)
{
player.playerRectangle.X -= player.playerSpeed;
}
else if (player.movingLeft)
{
player.playerRectangle.X += player.playerSpeed;
}
if (player.movingUp)
{
player.playerRectangle.Y += player.playerSpeed;
}
else if(player.movingDown)
{
player.playerRectangle.Y -= player.playerSpeed;
}
}
I have a method in Game1.cs
that's called "isColliding", which checks for collision in another class rectangle and my playerRectangle..
I'm sorry this is so long, but thank you in advance, and tell me if you need to know something else :)
isColliding
value into another variable and NOT call it 4 times... And useelse
if the conditions are mutually exclusive. – DariuszboundingBox
property of every sprite separated, because it looks thatplayerRectangle
you use to move sprites. then you can freely editboundingBox
by adding offsets and use that to check collision. – Davor Mlinaric