Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 22 additions & 25 deletions Samples/SimpleScene/Assets/Scripts/Physics.lua
Original file line number Diff line number Diff line change
@@ -1,62 +1,60 @@
local ecs = require "ecs"

local function rand_flt(from, to)
local maxNumber = 32767
local maxNumber = 32767
return from + (math.random(maxNumber) / maxNumber) * (to - from)
end

local function move(it)
for pos, vel, ent in ecs.each(it) do
pos.x = pos.x + vel.x * it.delta_time
pos.y = pos.y + vel.y * it.delta_time
pos.z = pos.z + vel.z * it.delta_time
pos.z = pos.z + vel.z * it.delta_time
end
end

local function gravity(it)
for pos, vel, grav, plane, ent in ecs.each(it) do
local planeEpsilon = 0.1

if plane.x * pos.x + plane.y * pos.y + plane.z * pos.z < plane.w + planeEpsilon then
do return end
end

vel.x = vel.x + grav.x * it.delta_time
vel.y = vel.y + grav.y * it.delta_time
vel.z = vel.z + grav.z * it.delta_time

if plane.x * pos.x + plane.y * pos.y + plane.z * pos.z > plane.w + planeEpsilon then
vel.x = vel.x + grav.x * it.delta_time
vel.y = vel.y + grav.y * it.delta_time
vel.z = vel.z + grav.z * it.delta_time
end
end
end

local function FrictionSystem(it)
for vel, friction, ent in ecs.each(it) do
vel.x = vel.x - vel.x * friction.value * it.delta_time
vel.y = vel.y - vel.y * friction.value * it.delta_time
vel.z = vel.z - vel.z * friction.value * it.delta_time
vel.y = vel.y - vel.y * friction.value * it.delta_time
vel.z = vel.z - vel.z * friction.value * it.delta_time
end
end

local function ShiverSystem(it)
for pos, shiver, ent in ecs.each(it) do
pos.x = pos.x + rand_flt(-shiver.value, shiver.value)
pos.y = pos.y + rand_flt(-shiver.value, shiver.value)
pos.z = pos.z + rand_flt(-shiver.value, shiver.value)
pos.y = pos.y + rand_flt(-shiver.value, shiver.value)
pos.z = pos.z + rand_flt(-shiver.value, shiver.value)
end
end

local function BounceSystem(it)
for pos, vel, plane, bounciness, ent in ecs.each(it) do
local dotPos = plane.x * pos.x + plane.y * pos.y + plane.z * pos.z
local dotVel = plane.x * vel.x + plane.y * vel.y + plane.z * vel.z
if dotPos < plane.w then
pos.x = pos.x - (dotPos - plane.w) * plane.x
pos.y = pos.y - (dotPos - plane.w) * plane.y
pos.z = pos.z - (dotPos - plane.w) * plane.z
local dotVel = plane.x * vel.x + plane.y * vel.y + plane.z * vel.z

if dotPos < plane.w then
pos.x = pos.x - (dotPos - plane.w) * plane.x
pos.y = pos.y - (dotPos - plane.w) * plane.y
pos.z = pos.z - (dotPos - plane.w) * plane.z

vel.x = vel.x - (1.0 + bounciness.value) * plane.x * dotVel
vel.y = vel.y - (1.0 + bounciness.value) * plane.y * dotVel
vel.z = vel.z - (1.0 + bounciness.value) * plane.z * dotVel
end
vel.x = vel.x - (1.0 + bounciness.value) * plane.x * dotVel
vel.y = vel.y - (1.0 + bounciness.value) * plane.y * dotVel
vel.z = vel.z - (1.0 + bounciness.value) * plane.z * dotVel
end
end
end

Expand All @@ -65,4 +63,3 @@ ecs.system(gravity, "grav", ecs.OnUpdate, "Position, Velocity, Gravity, BouncePl
ecs.system(FrictionSystem, "FrictionSystem", ecs.OnUpdate, "Velocity, FrictionAmount")
ecs.system(ShiverSystem, "ShiverSystem", ecs.OnUpdate, "Position, ShiverAmount")
ecs.system(BounceSystem, "BounceSystem", ecs.OnUpdate, "Position, Velocity, BouncePlane, Bounciness")