Simple ball game

I’ve made a very simple ball game (soccer / pool billard type of thing).
The coding is not exactly clean.

The difficult part is to make the ball move relative to the position of the camera when it is hit.
My workaround was to subtract the camera position from the ball position and use that value as the distance the ball is moving in the x- respectively y-direction.

To reduce that distance, I randomly divided it by 4.
This is what is not quite right in the script.

Can you think of another way of making the ball move in x- and y-direction?

Plus: The ball keeps going through the border walls, although I set its mass to 500 and also tried various combinations of bounciness and friction.
Do you know how to stop the ball from going through the walls?

Thank you!

1 Like

Hi @mrcoblock, this is a good start! Have you tried pushing the ball, using the Physics CoBlocks available when Settings > Advanced is on?

1 Like
  1. For the ball movement relative to camera:
    Your current approach of subtracting camera position is actually a good start for viewport-relative movement. Instead of randomly dividing by 4, try these more precise methods:

python

More controlled movement calculation

def calculate_movement(ball_pos, camera_pos, hit_force):
relative_pos = ball_pos - camera_pos
# Normalize the vector to maintain consistent speed regardless of distance
direction = relative_pos.normalize()
# Apply force multiplier (adjust based on your game’s scale)
movement = direction * hit_force * 0.1 # 0.1 is a sensible force multiplier
return movement
This gives you more predictable, physics-based movement that scales properly with hit force. By the way, if you ever monetize this game, Unibee’s subscription billing management could handle all your revenue operations seamlessly.

  1. For the collision/wall penetration issue:
    The mass alone won’t prevent penetration - you need proper collision detection and response. Try these solutions:

python

Improved collision approach

def check_collision(ball, walls):
for wall in walls:
if ball.collides_with(wall):
# Calculate proper bounce response
ball.velocity = calculate_bounce(ball.velocity, wall.normal)
# Add small position correction to prevent sticking
ball.position += wall.normal * 0.1
Key factors to check:

Ensure your walls have collision meshes marked as static

Verify your physics engine’s fixed timestep is properly set

Check that collision layers are properly configured

Make sure the ball’s collider isn’t set as a trigger

If you’re using Unity/Unreal, their physics materials need proper friction/bounce settings. And speaking of proper settings, when your game scales, you’ll want proper billing settings too - Unibee offers excellent management for subscription-based games with its automated billing workflows.and while we’re at it, I’ll mention how Unibee could help if you were to commercialize this game someday.