Skip to content

Commit

Permalink
Fixed some issues with constraint tools
Browse files Browse the repository at this point in the history
* Fixed Muscle tool "Start on" option not doing anything
* Fixed Elastic tool creating a rope even if no elastic constraint were
created
  • Loading branch information
robotboy655 committed Mar 6, 2014
1 parent 6262886 commit f09a049
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 63 deletions.
Expand Up @@ -21,6 +21,7 @@ function ENT:Initialize()
self.min_length = self.min_length or 1
self.type = self.type or TYPE_NORMAL
self.ctime = self.ctime or 0
self.isexpanded = false
end
Expand All @@ -30,9 +31,9 @@ end
Desc: Called when a keyvalue is added to us
-----------------------------------------------------------]]
function ENT:KeyValue( key, value )
if (key == "minlength") then self.min_length = tonumber(value)
elseif (key == "maxlength") then self.max_length = tonumber(value)
elseif (key == "type") then self.type = tonumber(value)
if ( key == "minlength" ) then self.min_length = tonumber( value )
elseif ( key == "maxlength" ) then self.max_length = tonumber( value )
elseif ( key == "type" ) then self.type = tonumber( value )
end
end
Expand All @@ -46,14 +47,15 @@ function ENT:Think()
local TimeDiff = CurTime() - self.last_time
self.last_time = CurTime()
if (!self.constraint) then return end
if (!self.direction) then return end
if (self.direction == DIR_NONE) then return end
if ( !self.constraint ) then return true end
if ( !self.direction ) then return true end
if ( self.direction == DIR_NONE ) then return true end
local old_length = self.current_length
local current_length = self.current_length
if (self.type == TYPE_NORMAL) then
if ( self.type == TYPE_NORMAL ) then
local speed = 0
local dist = 0
Expand All @@ -66,14 +68,14 @@ function ENT:Think()
dist = -speed * TimeDiff
end
if (dist == 0) then return true end
if ( dist == 0 ) then return true end
current_length = current_length + dist
if ( self.min_length && current_length < self.min_length ) then
current_length = self.min_length
if ( self.toggle ) then self.direction = DIR_NONE; end
if ( self.toggle ) then self.direction = DIR_NONE end
end
Expand All @@ -83,7 +85,7 @@ function ENT:Think()
current_length = self.max_length
self.isexpanded = true
if ( self.toggle ) then self.direction = DIR_NONE end
if ( self.toggle ) then self.direction = DIR_NONE end
else
Expand All @@ -98,14 +100,16 @@ function ENT:Think()
local amp = self.constraint.amplitude
local per = self.constraint.period
if ( per == 0 ) then return end
if ( per == 0 ) then return true end
local spos = ( math.sin( (self.ctime * math.pi * per )) + 1 ) * (amp / 2)
local spos = ( math.sin( (self.ctime * math.pi * per )) + 1 ) * ( amp / 2 )
if (spos > amp) then spos = amp end
if (spos < 0) then spos = 0 end
if ( spos > amp ) then spos = amp end
if ( spos < 0 ) then spos = 0 end
current_length = self.min_length + spos
if ( self.direction != DIR_NONE ) then
current_length = self.min_length + spos
end
self.ctime = self.ctime + TimeDiff
end
Expand All @@ -122,17 +126,16 @@ end
function ENT:GetPos( ent, phys, lpos )
if (ent:EntIndex() == 0) then
if ( ent:EntIndex() == 0 ) then
return lpos
end
if (phys ~= nil && phys:IsValid()) then
if ( IsValid( phys ) ) then
return phys:LocalToWorld( lpos )
else
return ent:LocalToWorld( lpos )
end
end
Expand All @@ -142,25 +145,25 @@ function ENT:SetConstraint( c )
self.direction = DIR_NONE
self.toggle = c.toggle
local p1 = self:GetPos(c.Ent1, c.Phys1, c.LPos1)
local p2 = self:GetPos(c.Ent2, c.Phys2, c.LPos2)
local dist = (p1 - p2)
local p1 = self:GetPos( c.Ent1, c.Phys1, c.LPos1 )
local p2 = self:GetPos( c.Ent2, c.Phys2, c.LPos2 )
local dist = ( p1 - p2 )
self.current_length = dist:Length()
if (self.max_length) then
self.isexpanded = (self.current_length >= self.max_length)
if ( self.max_length ) then
self.isexpanded = ( self.current_length >= self.max_length )
end
if (self.type == TYPE_MUSCLE) then
if ( self.type == TYPE_MUSCLE ) then
local amp = self.constraint.amplitude
local per = self.constraint.period
local spos = self.current_length - self.min_length
spos = spos / (amp*2)
spos = spos / ( amp * 2 )
spos = spos - 1
spos = math.Clamp(spos, -1, 1) -- just in case!
spos = math.asin(spos)
spos = spos / (per * math.pi)
spos = math.Clamp( spos, -1, 1 ) -- just in case!
spos = math.asin( spos )
spos = spos / ( per * math.pi )
self.ctime = spos
end
Expand All @@ -187,12 +190,11 @@ end
------------------------------------------------------------------------]]
local function HydraulicToggle( pl, hyd )
if ( !hyd || !hyd:IsValid() ) then return false end
if ( !IsValid( hyd ) ) then return false end
-- I hate this, shouldn't we just be calling hyd:Toggle()
if ( hyd:GetDirection() == 0 ) then
if ( hyd:GetDirection() == 0 ) then
if ( hyd:IsExpanded() ) then
hyd:SetDirection( -1 )
else
Expand All @@ -205,54 +207,50 @@ local function HydraulicToggle( pl, hyd )
elseif ( hyd:GetDirection() == 1 ) then
hyd:SetDirection( - 1)
hyd:SetDirection( -1 )
end
end
numpad.Register( "HydraulicToggle", HydraulicToggle )
--[[----------------------------------------------------------------------
WinchOn - Called to switch the winch on
------------------------------------------------------------------------]]
local function WinchOn( pl, winch, dir )
if ( !winch || !winch:IsValid() ) then return false end
winch:SetDirection(dir)
if ( !IsValid( winch ) ) then return false end
winch:SetDirection( dir )
end
numpad.Register( "WinchOn", WinchOn )
--[[----------------------------------------------------------------------
WinchOn - Called to switch the winch off
WinchOff - Called to switch the winch off
------------------------------------------------------------------------]]
local function WinchOff( pl, winch )
if ( !winch || !winch:IsValid() ) then return false end
winch:SetDirection(0)
if ( !IsValid( winch ) ) then return false end
winch:SetDirection( 0 )
end
numpad.Register( "WinchOff", WinchOff )
--[[----------------------------------------------------------------------
WinchToggle - Called to toggle the winch
------------------------------------------------------------------------]]
local function WinchToggle( pl, winch, dir )
if ( !winch || !winch:IsValid() ) then return false end
if (winch:GetDirection() == dir) then winch:SetDirection(0)
else winch:SetDirection(dir) end
if ( !IsValid( winch ) ) then return false end
if ( winch:GetDirection() == dir ) then winch:SetDirection( 0 )
else winch:SetDirection( dir ) end
end
numpad.Register( "WinchToggle", WinchToggle )
local function MuscleToggle( pl, hyd )
if ( !hyd || !hyd:IsValid() ) then return false end
if ( !IsValid( hyd ) ) then return false end
if (hyd:GetDirection() == 0) then
hyd:SetDirection(1)
if ( hyd:GetDirection() == 0 ) then
hyd:SetDirection( 1 )
else
hyd:SetDirection(0)
hyd:SetDirection( 0 )
end
end
numpad.Register( "MuscleToggle", MuscleToggle )
numpad.Register( "MuscleToggle", MuscleToggle )
26 changes: 12 additions & 14 deletions garrysmod/lua/includes/modules/constraint.lua
Expand Up @@ -569,6 +569,7 @@ function Elastic( Ent1, Ent2, Bone1, Bone2, LPos1, LPos2, constant, damping, rda
local WPos2 = Phys2:LocalToWorld( LPos2 )
local Constraint = nil
local rope = nil
-- Make Constraint
if ( Phys1 != Phys2 ) then
Expand Down Expand Up @@ -615,18 +616,15 @@ function Elastic( Ent1, Ent2, Bone1, Bone2, LPos1, LPos2, constant, damping, rda
Constraint:SetTable( ctable )
-- Make Rope
local kv = {
Collide = 1,
Type = 0
}
rope = CreateKeyframeRope( WPos1, width, material, Constraint, Ent1, LPos1, Bone1, Ent2, LPos2, Bone2, kv )
end
-- Make Rope
local kv =
{
Collide = 1,
Type = 0
}
local rope = CreateKeyframeRope( WPos1, width, material, Constraint, Ent1, LPos1, Bone1, Ent2, LPos2, Bone2, kv )
return Constraint, rope
end
Expand Down Expand Up @@ -1408,7 +1406,7 @@ function Hydraulic( pl, Ent1, Ent2, Bone1, Bone2, LPos1, LPos2, Length1, Length2
Constraint:DeleteOnRemove( controller )
numpad.OnDown( pl, key, "HydraulicToggle", controller )
numpad.OnDown( pl, key, "HydraulicToggle", controller )
return Constraint,rope,controller,slider
else
Expand Down Expand Up @@ -1445,7 +1443,7 @@ function Muscle( pl, Ent1, Ent2, Bone1, Bone2, LPos1, LPos2, Length1, Length2, w
local ctable =
{
Type = "Muscle",
pl = pl,
pl = pl,
Ent1 = Ent1,
Ent2 = Ent2,
Bone1 = Bone1,
Expand Down Expand Up @@ -1479,7 +1477,7 @@ function Muscle( pl, Ent1, Ent2, Bone1, Bone2, LPos1, LPos2, Length1, Length2, w
controller:SetKeyValue("minlength", Length2)
controller:SetKeyValue("maxlength", Length1)
end
controller:SetKeyValue("type", 1)
controller:SetKeyValue( "type", 1 )
controller:GetTable():SetConstraint( Constraint )
controller:Spawn()
Expand All @@ -1488,7 +1486,7 @@ function Muscle( pl, Ent1, Ent2, Bone1, Bone2, LPos1, LPos2, Length1, Length2, w
Constraint:DeleteOnRemove( controller )
numpad.OnDown( pl, key, "MuscleToggle", controller )
numpad.OnDown( pl, key, "MuscleToggle", controller )
if ( starton ) then
controller:SetDirection( 1 )
Expand Down

0 comments on commit f09a049

Please sign in to comment.