Godot Version
4.5
Question
I am making a space game with momentum based movement and I am trying to make collisions where you bounce off of things. Right now, when I hit something light, I think it mostly works, but when I hit a planet(20000kg), I just stop.
Here’s my movement and collision code:
var target_acceleration = Vector3.ZERO
var relative_acceleration = Vector3.ZERO
if Input.is_key_pressed(KEY_W):
target_acceleration.z += (-acceleration*delta) #Applying axial acceleration, compensated for time since last tick
if Input.is_key_pressed(KEY_S):
target_acceleration.z += (acceleration*delta)#Applying axial acceleration, compensated for time since last tick
if Input.is_key_pressed(KEY_A):
target_acceleration.x += (-acceleration*delta)#Applying axial acceleration, compensated for time since last tick
if Input.is_key_pressed(KEY_D):
target_acceleration.x += (acceleration*delta)#Applying axial acceleration, compensated for time since last tick
if Input.is_key_pressed(KEY_CTRL):
target_acceleration.y += (-acceleration*delta)#Applying axial acceleration, compensated for time since last tick
if Input.is_key_pressed(KEY_SPACE):
target_acceleration.y += (acceleration*delta)#Applying axial acceleration, compensated for time since last tick
relative_acceleration = transform.basis * target_acceleration #Rotate movement to be relative to ship's orientation
target_velocity += relative_acceleration #Apply acceleration
target_velocity *= natural_deceleration #Natural deceleration
velocity = target_velocity
velocity += get_gravity() #Apply gravity
bounces = 0
remaining = velocity * delta
impact = move_and_collide(remaining) #Built in function to have nice movement, velocity, and collision
if impact != null:
velocity = velocity.bounce(impact.get_normal())
remaining = impact.get_remainder()
impact = null
impact = move_and_collide(remaining)
Thanks to pizza_delivery_man and hyvernox, here is fixed code:
var target_acceleration = Vector3.ZERO
var relative_acceleration = Vector3.ZERO
if Input.is_key_pressed(KEY_W):
target_acceleration.z += (-acceleration*delta) #Applying axial acceleration, compensated for time since last tick
if Input.is_key_pressed(KEY_S):
target_acceleration.z += (acceleration*delta)#Applying axial acceleration, compensated for time since last tick
if Input.is_key_pressed(KEY_A):
target_acceleration.x += (-acceleration*delta)#Applying axial acceleration, compensated for time since last tick
if Input.is_key_pressed(KEY_D):
target_acceleration.x += (acceleration*delta)#Applying axial acceleration, compensated for time since last tick
if Input.is_key_pressed(KEY_CTRL):
target_acceleration.y += (-acceleration*delta)#Applying axial acceleration, compensated for time since last tick
if Input.is_key_pressed(KEY_SPACE):
target_acceleration.y += (acceleration*delta)#Applying axial acceleration, compensated for time since last tick
relative_acceleration = transform.basis * target_acceleration #Rotate movement to be relative to ship's orientation
target_velocity += relative_acceleration #Apply acceleration
target_velocity *= natural_deceleration #Natural deceleration
velocity = target_velocity
velocity += get_gravity() * delta #Apply gravity
impact = move_and_collide(velocity*delta) #Built in function to have nice movement, velocity, and collision
if impact != null:
var collider = impact.get_collider()
var bounce = collider.physics_material_override.bounce
var impact_normal = impact.get_normal()
#impact_normal *= bounce
#impact_normal = impact_normal.normalized()
velocity = velocity.bounce(impact_normal)
velocity *= bounce
target_velocity = velocity
impact = null
impact = move_and_collide(velocity*delta)