How To: Make A Realistic Rolling Ball

howto

#1

Hello all!

I’d like to share a cool trick I found, on how to make a rolling ball.

At first I thought I’d make it so it decreases the X Velocity and the Y Velocity as it rolls but that causes the ball to roll in straight lines when it starts to slow down. Anyways here’s how (link to the project at the bottom.)

First thing first, you’ll need these variables for the ball:
•X
•Y
•X Velocity
•Y Velocity
•Velocity
•State (ball state)

Set these up:


You can set the ball size and text to whatever you’d like. If you take the If statement out of the “When Pressed” block it will make it so you can move the ball when it is still rolling.

Next, get this:


This will set the state to 2 when the player releases their finger off of the screen. Credit and thanks to @Funky_63_Greenland for this technic.

This will run when the player lifts their finger off of the screen. It sets the X Velocity and Y Velocity to a number in which is multiplied by the velocity. The X Velocity and Y Velocity only change when they hit a wall, but otherwise they don’t.


The rest of the velocity value:

Next:


Set the positions, and movement of the ball.

Next:


These slow down the ball.

This stops it, and sets the state to 0 so that the player can move the back again.

And finally, the bouncing off of the walls/borders.


Each one has a “When (State == 3)” before the And.

So when you drag on the ball, it should roll, bounce off the borders and come to a gradual stop.

Completed project if you’d like to see it and/or try it yourself:


#2

Nice tutorial, but I don’t have HS anymore I am sorry


#3

Cool tutorial, I might use it later!


#4

Thanks for the tutorial!

This is super helpful and I will totally need this later!!!


#5

Idk if it’s possible but it would be really cool to see 1 or 2 balls added to the game that would bounce off of each other using this technique


#6

I am working on that, I want to see if there is a way without making it so I don’t have to make things like “When self bumps Ball 1” but I can’t think of any other way. And if I do that it will get really laggy in the editor.


#8

Cool project, but try simplifying code a little


#9

The only thing I could simplify some of the cody is by taking out the “When State = 2” block and putting in the other one for when the player lets go of the screen. The reason why everything else is so split up or there are 3 different “When State = 3” blocks is because if you put them all in the same block it creates some lag to the ball movement. When put in separate ones it doesn’t.

So I wish I could simplify the code but I’m just not sure how without making it lag.


#10

Awesome project! The physics look so realistic!


#11

Hi @Madi_Hopscotch_ I have no one to speak too.


#12

Hi! :smiley: I am on my way to school so I can’t talk for long :joy:


#13

OOOOOF. Makes sense. Well. Um. I got a new project that’s good?


#15

That’s a lot of code, but a really good explanation and tutorial! :+1:


#16

Great tutorial! I think this will benefit many people!


#17

I agree with everyone else!

The fact that you made this is AWESOME, GREAT, and HELPFUL!
But I think if your code could be simplified, so anyone can re-create it, that would make it EVEN better!