Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | akhenaten | |
Old Version | Published before Godot 3 was released. |
Hello!
I’m writing a simple Breakout style game, and have encountered an odd issue. I am using my own code for the movement and bouncing of the ball, but I’m using Godot’s collision: the ball is a KinematicBody2D
and CollisionShape2D,
and the bricks are StaticBody2D
’s and CollisionShape2D
’s.
The collision/bounce code (written for the ball) looks something like this:
if (is_colliding()):
var n = get_pos() - get_collision_pos()
print('n = ', n)
n = n.normalized()
var a = moveVector.dot(n)
var p = (2.0 * a)
newMovement = moveVector - p * n
The issue I’m having is with n
. When the ball deflects off a horizontal surface, the collision normal should be n = (0, 31)
, where 31 is the radius of the ball. Instead, I get results logged that look like this:
n = (-0.976074, 31.36499)
n = (-31.96582, 0.304688)
n = (-0.880859, -30.671875)
n = (-0.879395, 31.371582)
n = (-0.881592, -30.671631)
n = (-0.88147, 31.355103)
n = (-0.881836, -30.683838)
n = (-0.881165, 31.378418)
The x/y values are always offset just a bit, and the result is very bizarre bouncing physics. Is this just expected with Godot’s collision system, or am I missing something simple?
I worked around the problem with what amounts to if (x < 1): x = 0, y = 31
but I’d love to know why the problem is happening in the first place.
Thanks for any help!