Why Isn’t My Ball Bouncing Correctly with an Integer Velocity?

0
1
Asked By CodingNerd101 On

I'm trying to make a ball bounce using some physics in my game, but I'm running into issues with my velocity variable. I set up "int vel_y = 5;" for the vertical velocity, but it seems like the ball isn't bouncing properly. I noticed that if I change this to a float, it works fine. Can anyone explain what's happening with using an integer for velocity and why it affects the bounce? Also, what happens to the values during the calculations? Here's a quick snippet of my code:

```
int vel_y = 5;
float gravity = 0.3f;

while(!WindowShouldClose()){
BeginDrawing();
ClearBackground((Color) {0, 0, 0, 255});
DrawCircle(ball_x, ball_y, ball_radius, (Color){255, 255, 255, 255});
vel_y += gravity;
ball_y += vel_y;

if (ball_y >= (screen_h - ball_radius)) {
ball_y = screen_h - ball_radius;
vel_y = -vel_y * 0.8f;
}
}
```

Also, here's a link to an image showing what happens:
[Image](https://imgur.com/a/oxR07xf)

5 Answers

Answered By TechieTom On

The issue with your integer velocity is that when you add gravity (which is a float), the result gets converted back to an integer when you reassign it to "vel_y". So, if you start with 5 and add 0.3, it actually stays 5 because integers can’t handle decimals—they just truncate the fraction. This means your ball won't actually change its speed as it should when it falls.

Also, when it bounces, it starts getting negative values, which eventually get truncated to zero, leading to your ball stopping entirely. Switching to a float for your velocity will let you capture those decimal values and result in proper bouncing behavior!

Answered By PixelPusher On

It's all about how data types handle numbers! Integers discard anything past the decimal point, so after adding gravity, your velocity isn't really changing. If you need finer control for physics, definitely go for floats across the board!

Answered By GameDevGuru On

You're running into a classic problem with integer math. In your case, adding the float gravity to the int vel_y results in truncation of any decimal part when you store it back into an integer. This happens because of how integers work—they can't store fractions. So, your intended acceleration due to gravity isn't taking effect properly. Using floats for both your velocity and gravity will definitely help get the results you're looking for.

If you want that bounce to be smooth and realistic, definitely stick with floats!

floatFanatic -

Exactly! Those decimal values are crucial for physics in games; without them, you'll end up with choppy motion.

Answered By CodeCracker42 On

An int won't allow decimal values. When you add 0.3 to your velocity, it just rounds it back down to the nearest whole number, effectively keeping it at 5, so the ball will just fall straight down without any real bounce.

Switching to a float will fix this and let your physics calculations work as expected!

Answered By DevDude98 On

You're correct in observing the difference when you switch to float. When you assign vel_y to an int, it truncates any decimal, causing issues with realistic motion. This means that even when you're doing calculations that resulting in float values, they'll simply round down when assigned back to vel_y. Just use float throughout, and your ball should bounce as expected!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.