Team Spartan Cookies & Milk Forums
Issue with applyForceOffset - Printable Version

+- Team Spartan Cookies & Milk Forums (https://teamscm.co.uk)
+-- Forum: Garry's Mod (https://teamscm.co.uk/forum-4.html)
+--- Forum: Help and Support (https://teamscm.co.uk/forum-8.html)
+--- Thread: Issue with applyForceOffset (/thread-894.html)



Issue with applyForceOffset - LocInt - 28-04-2015

Hey,

For some reason starting today im getting this error:

Code:
sv: SF:starfall/loctech/lift.txt:204: Type mismatch (Expected Vector, got NaN at index 1) in function applyForceOffset

Line 204: I've been using PhysObj:applyForceOffset( Dir, Offset ). Dir and Offset come from calculations of applyTorque.

This issue wasnt here last night. Also applyForceOffset is acting weird.


RE: Issue with applyForceOffset - Person8880 - 29-04-2015

You're getting the normalised version of a zero vector, i.e dividing by 0.

Somewhere in the torque code there's
off = off:GetNormal() * torqueamount * 0.5

local dir = ( tq:Cross( off ) ):GetNormal()
If off is the zero vector, or tq and off are the same vector (which causes the cross product to return the zero vector) you end up with nan, nan, nan. There's also the case of where tq is the zero vector which will also cause the problem.

Basically, check for:
if torqueamount == 0 then return end
-- carry on...

EDIT: Had more than a 2 second look. The only way you'd end up with 0 magnitude vectors below is if your input is zero. So just check for zero torque.


RE: Issue with applyForceOffset - LocInt - 29-04-2015

(29-04-2015, 05:48 PM)Person8880 Wrote: You're getting the normalised version of a zero vector, i.e dividing by 0.

Somewhere in the torque code there's
off = off:GetNormal() * torqueamount * 0.5

local dir = ( tq:Cross( off ) ):GetNormal()
If off is the zero vector, or tq and off are the same vector (which causes the cross product to return the zero vector) you end up with nan, nan, nan. There's also the case of where tq is the zero vector which will also cause the problem.

Basically, check for:
if torqueamount == 0 then return end
-- carry on...

EDIT: Had more than a 2 second look. The only way you'd end up with 0 magnitude vectors below is if your input is zero. So just check for zero torque.

Want to explain why this started to happen just now not before? Seriously I don't have time to fix all my code everyday. Sorry for being so rude but I feel like fixing all my chips always when I come online.


RE: Issue with applyForceOffset - Person8880 - 29-04-2015

(29-04-2015, 06:39 PM)LocInt Wrote: Want to explain why this started to happen just now not before? Seriously I don't have time to fix all my code everyday. Sorry for being so rude but I feel like fixing all my chips always when I come online.
It's a property of mathematics? You're passing a zero vector in, you're going to get garbage out. The error is just stopping you from wasting time with something that wouldn't work.


RE: Issue with applyForceOffset - LocInt - 29-04-2015

(29-04-2015, 06:44 PM)Person8880 Wrote:
(29-04-2015, 06:39 PM)LocInt Wrote: Want to explain why this started to happen just now not before? Seriously I don't have time to fix all my code everyday. Sorry for being so rude but I feel like fixing all my chips always when I come online.
It's a property of mathematics? You're passing a zero vector in, you're going to get garbage out. The error is just stopping you from wasting time with something that wouldn't work.

It actually worked just fine before I got this error. Big Grin


RE: Issue with applyForceOffset - Person8880 - 29-04-2015

(29-04-2015, 06:45 PM)LocInt Wrote: It actually worked just fine before I got this error. Big Grin
The point is to make you aware that you're doing something wrong, rather than silently failing. It "worked" before because when you passed in zero it did nothing.


RE: Issue with applyForceOffset - LocInt - 04-05-2015

This issue seems to be back. I have all these

if(Torqueamount == 0)then return end

if(Offset:IsZero()) then return end

if(Dir:IsZero())then return end

but yet sv: SFConfusedtarfall/loctech/lift.txt:209: Type mismatch (Expected Vector, got NaN at index 1) in function applyForceOffset

Line 209: PhysObj:applyForceOffset( Dir, Offset )


RE: Issue with applyForceOffset - Person8880 - 04-05-2015

Check your force amount, I bet it'll print out as something like #Q.NAND, which is Source's way of saying way too big.

Basically you're probably passing "infinite" force to it. Just clamp the amount of torque you're applying:
local MaxTorque = 5e6 --Who knows what a good max is, try them and see
local NewTorqueAmount = math.clamp( TorqueAmount, 0, MaxTorque )
if NewTorqueAmount == 0 then return end
if NewTorqueAmount ~= TorqueAmount then
Torque:Normalize()
Torque:Mul( NewTorqueAmount )
TorqueAmount = NewTorqueAmount
end