javaandroideclipselibgdx

Bouncing sprite off walls when sprite is rotating with accelerometer


I have a sprite that rotates based on the pitch and roll of the device. The code that I use to rotate the sprite is:

double myRadians = Math.atan2(-Gdx.input.getRoll(), Gdx.input.getPitch());
float myDegress = Math.round(((myRadians*180)/Math.PI));

I also set the position of the sprite like so:

float yChange = Math.round(Gdx.input.getRoll());
float xChange = Math.round(-Gdx.input.getPitch());
float yMove = Math.round(yChange/1.5);
float xMove = Math.round(xChange/1.5);

Now, when ever this sprite object collides with a "wall" I want it to bounce back. But mirrored first. I have done that like so:

// mirror sprite
// pitchAndRoll is a Vector2 and set to the pitch and roll 
// at the time of collision
float flippedRotation = sprite.getRotation() * -1;

float yChange = Math.round(pitchAndRoll.y);
float xChange = Math.round(-pitchAndRoll.x);

if (whichWall.equals(Walls.WALL_LEFT)){
    // left wall        

    flippedRotation = sprite.getRotation() * -1;

    xChange = Math.round(pitchAndRoll.x);
}else if (whichWall.equals(Walls.WALL_RIGHT)){
    // right wall   

    flippedRotation = sprite.getRotation() * -1;

    xChange = Math.round(pitchAndRoll.x);
}else if (whichWall.equals(Walls.WALL_BOTTOM)){
    // bottom wall

    flippedRotation += 180;                 

    yChange = Math.round(-pitchAndRoll.y);
}else if (whichWall.equals(Walls.WALL_TOP_LEFT)){
    // top left wall        

    flippedRotation += 180;     

    yChange = Math.round(-pitchAndRoll.y);
}else if (whichWall.equals(Walls.WALL_TOP_RIGHT)){
    // top right wall   

    flippedRotation += 180;     

    yChange = Math.round(-pitchAndRoll.y);
}

The walls are all perfectly horizontal and vertical, respectively.

The first collision is fine, any other collisions that occur, the sprite just goes right through the walls.

But my main issue is that as the sprite is controlled using the device's accelerometer, if the user has the device pointing downwards, the sprite will move down fine, hit the wall, bounce back, then move back down and the loop continues. I want to prevent this from happening but can't think how.

It's hard to explain so I here are some images that hopefully make more sense =]]

Image 1 Image 2 Image 3 Image 4 Image 5

I hope these pictures give you a better understanding of what I am trying to do and if you could in anyway, help me, or point me into the right direction, or any alternatives would be amazing!! Thanks in advance.


Solution

  • The way I have "fixed" it, is by checking if the device's tilt has changed from the point of collision, if not the sprite continues to move in the direction it was bounced off from the wall, until the device's tilt changes.

    Thanks for the help.