This site requires JavaScript, please enable it in your browser!
Greenfoot back
pio22mibi
pio22mibi wrote ...

2019/12/3

How to I make "Gravity"?

pio22mibi pio22mibi

2019/12/3

#
I am making a game where I have a ball that falls wherever you "dig". How do i make the ball fall downwards as if there was gravity?
Gamingprotramp7 Gamingprotramp7

2019/12/3

#
private final int GRAVITY = 1; //gravity. to make it fall. Final int so it cant be changed unless you hard code it. private int velocity; //optional. makes it fall faster over time. makes it more realistic. public void fall() //fall. method for gravity to work { setLocation(getX(), getY() + velocity); //makes character fall depending on velocity if (isOnSolidGround()) //checks if boolean isOnSolidGround() is true { velocity = 0; //sets velocity to 0 if its on solid ground so the character doesnt fall through objects while (isOnSolidGround()) //fixes bug { setLocation(getX(), getY() - 1); } setLocation(getX(), getY() + 1); } else velocity += GRAVITY; //makes character fall if not on solid ground. doubles gravity over time to make it more realistic } public boolean isOnSolidGround() //checks if character is on solid ground { boolean isOnGround = false; int imageWidth = getImage().getWidth(); int imageHeight = getImage().getHeight(); if (getOneObjectAtOffset(imageWidth / -2, imageHeight / 2, Platform.class) != null || getOneObjectAtOffset(imageWidth / 2, imageHeight / 2, Platform.class) != null) isOnGround = true; //checks if ball is on a platform (change "platform" to name of the platform you have ex: ground, floor, etc.) return isOnGround; //returns so that it knows when to stop making the character fall } //I hope this helped you. If there are any problems with it you can ask me because i made the code to be gravity for a character not a ball so i had to change a few things. Also don't forget to change the names so it matches up and doesn't break.
Gamingprotramp7 Gamingprotramp7

2019/12/3

#
Gamingprotramp7 wrote...
private final int GRAVITY = 1; //gravity. to make it fall. Final int so it cant be changed unless you hard code it. private int velocity; //optional. makes it fall faster over time. makes it more realistic. public void fall() //fall. method for gravity to work { setLocation(getX(), getY() + velocity); //makes character fall depending on velocity if (isOnSolidGround()) //checks if boolean isOnSolidGround() is true { velocity = 0; //sets velocity to 0 if its on solid ground so the character doesnt fall through objects while (isOnSolidGround()) //fixes bug { setLocation(getX(), getY() - 1); } setLocation(getX(), getY() + 1); } else velocity += GRAVITY; //makes character fall if not on solid ground. doubles gravity over time to make it more realistic } public boolean isOnSolidGround() //checks if character is on solid ground { boolean isOnGround = false; int imageWidth = getImage().getWidth(); int imageHeight = getImage().getHeight(); if (getOneObjectAtOffset(imageWidth / -2, imageHeight / 2, Platform.class) != null || getOneObjectAtOffset(imageWidth / 2, imageHeight / 2, Platform.class) != null) isOnGround = true; //checks if ball is on a platform (change "platform" to name of the platform you have ex: ground, floor, etc.) return isOnGround; //returns so that it knows when to stop making the character fall } //I hope this helped you. If there are any problems with it you can ask me because i made the code to be gravity for a character not a ball so i had to change a few things. Also don't forget to change the names so it matches up and doesn't break.
i forgot to mention you can also check out the game i made. it is the first version so all it is is a dolphin that you can control with the left and right arrow keys and space bar to make it jump
You need to login to post a reply.