0
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$
    – Lewissss
    Mar 4, 2013 at 13:03

1 Answer 1

2
\$\begingroup\$

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.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .