mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-26 23:54:34 +00:00
[added] more intersection/resolution helpers
This commit is contained in:
parent
bfad0baba9
commit
c840bd01a5
@ -420,13 +420,58 @@ end
|
|||||||
|
|
||||||
--resolution helpers
|
--resolution helpers
|
||||||
|
|
||||||
|
--resolve a collision between two bodies, given a (minimum) separating vector
|
||||||
|
-- from a's frame of reference, like the result of any of the _collide functions
|
||||||
|
--requires the two positions of the bodies, the msv, and a balance factor
|
||||||
|
--balance should be between 1 and 0;
|
||||||
|
-- 1 is only a_pos moving to resolve
|
||||||
|
-- 0 is only b_pos moving to resolve
|
||||||
|
-- 0.5 is balanced between both (default)
|
||||||
|
--note: this wont work as-is for line segments, which have two separate position coordinates
|
||||||
|
-- you will need to understand what is going on and move the second coordinate yourself
|
||||||
|
function intersect.resolve_msv(a_pos, b_pos, msv, balance)
|
||||||
|
balance = balance or 0.5
|
||||||
|
a_pos:fmai(msv, balance)
|
||||||
|
b_pos:fmai(msv, -(1 - balance))
|
||||||
|
end
|
||||||
|
|
||||||
--bounce a velocity off of a normal (modifying velocity)
|
--bounce a velocity off of a normal (modifying velocity)
|
||||||
local _bounce_temp = vec2()
|
--essentially flips the part of the velocity in the direction of the normal
|
||||||
function intersect.bounce_off(velocity, normal)
|
function intersect.bounce_off(velocity, normal, conservation)
|
||||||
_bounce_temp:vset(velocity)
|
--(default)
|
||||||
|
conservation = conservation or 1
|
||||||
|
--take a copy, we need it
|
||||||
|
local old_vel = vec2.pooled_copy(velocity)
|
||||||
|
--reject on the normal (keep velocity tangential to the normal)
|
||||||
velocity:vreji(normal)
|
velocity:vreji(normal)
|
||||||
velocity:vsubi(_bounce_temp:vsubi(velocity))
|
--add back the complement of the difference;
|
||||||
|
--basically "flip" the velocity in line with the normal.
|
||||||
|
velocity:fmai(old_vel:vsubi(velocity), -conservation)
|
||||||
|
--clean up
|
||||||
|
old_vel:release()
|
||||||
return velocity
|
return velocity
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--mutual bounce; two similar bodies bounce off each other, transferring energy
|
||||||
|
function intersect.mutual_bounce(velocity_a, velocity_b, normal, conservation)
|
||||||
|
--(default)
|
||||||
|
conservation = conservation or 1
|
||||||
|
--take copies, we need them
|
||||||
|
local old_a_vel = vec2.pooled_copy(velocity_a)
|
||||||
|
local old_b_vel = vec2.pooled_copy(velocity_b)
|
||||||
|
--reject on the normal
|
||||||
|
velocity_a:vreji(normal)
|
||||||
|
velocity_b:vreji(normal)
|
||||||
|
--calculate the amount remaining from the old velocity
|
||||||
|
--(transfer ownership)
|
||||||
|
local a_remaining = old_a_vel:vsubi(velocity_a)
|
||||||
|
local b_remaining = old_b_vel:vsubi(velocity_b)
|
||||||
|
--transfer it to the other body
|
||||||
|
velocity_a:fmai(b_remaining, conservation)
|
||||||
|
velocity_b:fmai(a_remaining, conservation)
|
||||||
|
--clean up
|
||||||
|
a_remaining:release()
|
||||||
|
b_remaining:release()
|
||||||
|
end
|
||||||
|
|
||||||
return intersect
|
return intersect
|
||||||
|
Loading…
Reference in New Issue
Block a user