Page 1 of 1

Collision resolving not behaving as expected

Posted: Thu Feb 03, 2022 12:58 pm
by Gunroar:Cannon()
The movement isn't working as expected.

The friction between the player and the floor is really low, so when the player moves it should move crazy fast, both against a wall verticaly and when it moves horizontally.
But the player moves normally horizontally and only moves crazy fast vertically when it moves against a wall (even if it has no y velocity it gains some by just bumping into the wall).

Why does the friction/collision resolution not work as it should?
I'm stumped.

Code for resolving of collision
toybox/entites/Object.lua
~line 312

Code: Select all

function Object:__resolve_collision(instance, trash_separation_x, trash_separation_y, collision)
  --collision is collision data gotten from kikito's bump.lua
  
  local friction = instance.friction
  
  local normalX, normalY = collision.normal.x, collision.normal.y
  local tangentX, tangentY = -normalX, normalY
  -- relative velocity
  local vx = self.vx - (instance.vx or 0)
  local vy = self.vy - (instance.vy or 0)

  -- penetration speed
  local penetration_speed = vx * normalX + vy * normalY

  -- penetration component
  local penetration_x = normalX * penetration_speed
  local penetration_y = normalY * penetration_speed

  -- tangent component
  local tangent_x = vx - penetration_x 
  local tangent_y = vy - penetration_y

  -- restitution
  local restitution = 1 + math.max(self.bounce, instance.bounce or 0)

  -- friction
  local friction = math.max(self.friction, instance.friction or 0)
  
  -- change the velocity of shape a
   
  self.vx = vx - penetration_x * restitution + tangent_x * friction
  
  self.vy = vy - penetration_y * restitution + tangent_y * friction
end
Was coded on a touchscreen device but should work with keyboards too.
(kind of based of this)

But flip it, life's handing you lemons, make lemonade then Image