Reputation: 5495
How can I move a sprite left or right, if there is a slope?
If it was only a straight line, there will be something like
if(Pressed(Keys.Left))
position.x-=5;
if(Pressed(Keys.Right))
position.x+=5;
But what if i have something like this
How i do to tell the sprite to go up or down while going left or right? I'm using libGDX.
Upvotes: 0
Views: 1034
Reputation: 17283
I would suggest to use vectors.
The player will move using vectors. It will move according to it's movement vector. The movement vector will have a constant magnitude (aka length), so the player's speed will be constant. But it's direction will change.
The direction of the vector will be set according to the floor that the sprite is standing on.
If the current angle of the floor (relative to the x axis) is 0 or 180 degrees (aka parallel to the x axis), than the vector will also be parallel to the x axis.
If under the sprite there is a slope, the angle of the slope will be calculated, and a new movement vector will be created with the same angle, thus the sprite will always move parallel to the floor.
Advice on specific implementation (basic knowledge on vectors needed):
Create a Vector
class. This class will have two fields of type double
: x
and y
. They will be set in the class' constructor (public Vector(int a, int b)
). It will also have a normalize()
function.
In your class representing the player (e.g. Player
class), you need to add a field of type Vector
, named movementVector
. Every frame, get the point on the floor to the left of the point where the sprite currently touches the floor, and save it as Point a
. Also get the point to the right, and save it as Point b
.
Everyframe, set the movementVector
field to:
new Vector( b.getX() - a.getX(), b.getY() - a.getY() );
This gives you a vector in the desired angle of movement.
Then normalize the vector: movementVector.normalize()
.
Now, when proceeding to move the object, do this:
position_x += movementVector.getX() * h
position_y += movementVector.getY() * h
Where h
determines the speed of the player.
This obviously requires some previous basic knowledge on vectors. And also, you need to be able to get specific points on the floor.
I hope I don't have any error in this.
Good luck
Upvotes: 1
Reputation: 34528
If you have coordinates of the slope you can adjust main sprite coordinates according to it.
If not you can either create one by analyzing picture (check in loop for lowest blue pixel coordinate, store value to array)
Also you can opt for dynamic slope search. This gives you a benefit of automatic adjustment in case of small changes in the background.
Simplified example:
final int ELEVATION = 10; // how high above we slope we want to levitate
if(Pressed(Keys.Left)) {
position.x-=5;
int slopecoord = position.y; // you may want to start from 0 if you have steep ascents
// assuming your background is BufferedImage and has same size as playing screen
do {
slopecoord--;
Color c = new Color(bgImage.getRGB(position.x, slopecoord));
int red = c.getRed();
int green = c.getGreen();
int blue = c.getBlue();
//comparing colors takes some effort as your blue in image is not same over the picture
while( Math.abs(red - 48) < 25 && Math.abs(green - 158) < 25 && blue > 200 );
position.y = slopecoord - ELEVATION;
}
Upvotes: 1
Reputation: 17283
I suggest add gravity to your game. The gravity will always pull the sprite downwards (-5 on the y axis), but the collision with the floor will always stop the sprite. Unless there is a slope, and then the sprite won't collide with the floor and it will go down, and also in the direction of the keys pressed.
Upvotes: 1