0
votes

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 :)

3
omg, please store isColliding value into another variable and NOT call it 4 times... And use else if the conditions are mutually exclusive.Dariusz
Can you post isColliding() code?borkovski
@Dariusz or better yet, do something like if (IsColliding()) { // additional logic here }gleng
create boundingBox property of every sprite separated, because it looks that playerRectangle you use to move sprites. then you can freely edit boundingBox by adding offsets and use that to check collision.Davor Mlinaric

3 Answers

2
votes

i will write from my head... add this into your sprite class, it will be universal

public Rectangle BoundingBox
{
 get { return new Rectangle((int)Position.X + offsetX, (int)Position.Y + offsetY, Texture.Width - offsetWidth, Texture.Height - offsetWidth); }
}

simply changing offsetX, offsetY, offsetWidth, offSetHeight you can adjust collision box. so if you wish collision on top left corner for 5x5 pixels. then offsetX=0, offestY=0, offsetWidth=5, offsetHeight=5

and collide code

foreach (Sprite brick in bricklist)
{
   if (player.BoundingBox.Intersects(brick.BoundingBox))
   {
       // do some stuff
   }
}
1
votes

If you want to change the height of the player rectangle by half, all you do is just divide the playerRectangle.Y value by 2.

1
votes

Use Vector2.Distance(playerPosition, objectToCheck);

if(Vector2.Distance(playerPosition,objectToCheck < player.rectangle.width/2){
 //If  the player is too close to the object you wish to check
 //Do something...
}

For the collisions, i'd just check in your player's update method whether or not 'he' has collided with anything - using vector.distance^^

You're movement logic is bad:

You should be using velocity.

var velocity = new Vector2(0,0); var moveSpeed = 5; //Whatever you want

if(player presses right){
   velocity.X += moveSpeed;
}
if(player presses up){
   velocity.y += moveSpeed;
}

this.position += velocity * Gametime.elapsedGametime; << Think Gametime is correct...

Var origin = new vector2(rectangle.width /2, rectangle.height/2); This will set the origin of the sprite to the center of the rectangle, as opposed to the top left.

Say we have two rectangles both 50x50... to check for a collision between the two you'd do:

if(Vector2.Distance(rectangle1.position, rectangle2.position < rectangle1.width/2){

    //If the distance between the two rectangles (from the middle of the rectangle(the       origin we just set is less than then half of the width of the rectangle... we have a collision.
}

Haven't used XNA in a few months, so you might have to update the origin in the update method of your classes...