# How To: Make A Realistic Rolling Ball

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:

17 Likes

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

3 Likes

Cool tutorial, I might use it later!

3 Likes

Thanks for the tutorial!

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

3 Likes

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

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.

2 Likes

Cool project, but try simplifying code a little

1 Like

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.

Awesome project! The physics look so realistic!

5 Likes

Hi @Madi_Hopscotch I have no one to speak too.

1 Like

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

1 Like

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

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

2 Likes

Great tutorial! I think this will benefit many people!

1 Like

I agree with everyone else!