Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm just prototyping a simple project. I'm moving a sprite on screen using the Xbox gamepad:

public override void Update(GameTime gameTime)
    {
        gamePad = GamePad.GetState(PlayerIndex.One);
        GetPlayerRotation();      
        base.Velocity = new Vector2(vSpeed.X * gamePad.ThumbSticks.Left.X, -vSpeed.Y * gamePad.ThumbSticks.Left.Y);
        base.Update(gameTime);
    }

    private void GetPlayerRotation()
    {
        leftThumb = gamePad.ThumbSticks.Left;
        base.Rotation = -(float)Math.Atan2(leftThumb.Y, leftThumb.X);
    }

This works fine to move the sprite in the correct direction (Dependent on the angle of the joystick).

However, the problem is, when I try to do a collision with a simple test object that is just a sprite, with a rectangle:

        public virtual bool Collision(Entity entity1, Entity entity2)
    {
        if (entity1.Rectangle.Intersects(entity2.Rectangle))
        {
            Debug.WriteLine("Collision");
            return true;
        }
        else
            return false;
    }

            foreach (TestBlock block in blockList)
        {
            block.Update(gameTime);
            if(player.Collision(player, block))
            {
                player.vSpeed = -player.vSpeed;
            }
        }

Whilst this does work, and the speed is inverted, when the bool returns to false after the collision, the control scheme becomes inverted (I.e, pressing the thumbstick up moves the sprite down).

I was trying to achieve a simple bounce effect off the block.

share|improve this question
    
I should also add: after a second collision (Once the controls have already been inverted) the inversion is cancelled out. If it helps, i'm using a simple position += velocity in the update for movement. –  Lewissss Mar 4 '13 at 13:03
add comment

1 Answer

Your current collision response is flawed, since you are simply inverting the variable with your speed multiplier. The way you are using that speed multiplier in your Update() method would result in your controls being reversed.

What you need to do is create a collision response based on the two objects colliding. You can try to mimic the behavior by applying an impulse on the objects in the opposite direction:

entity.Velocity *= -(player.vSpeed * impulse);

which will give you a very poor collision response. This code is just an example. You can try to refine your own response.

Another option would be to use a physics engine, which would take many things into consideration, such as the mass, velocity, direction, rotation of both entities involved in the collision to provide a more realistic collision response.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.