PhysObj/ApplyForceCenter

From Garry's Mod
Revision as of 04:27, 29 May 2016 by Andrew900460 (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
 PhysObj:ApplyForceCenter( )

Contents

Description

Applies the specified force to the physics object. (in Newtons)

NOTE

This will not work on players, use Entity:SetVelocity instead.

Arguments

Vector force

The force to be applied.

Examples

Example

An entity that Simulates it's own gravity by applying a force downward on the entity based on the force equation.

(Force = mass * acceleration)

Since, by default, entities already have gravity. The default gravity must be turned off by adding "phys:EnableGravity(false)" in the entities Initialize function so that the default gravity doesn't interfere with our custom gravity.

NOTE: We can get the mass of the entity by using the GetMass() function.


NOTE
-9.80665 (meters / second^2)  Is the approximate acceleration of objects on Earth due to gravity. (It is negative because gravity pushes things downwards.)
function ENT:Initialize()
    self:SetModel("models/hunter/blocks/cube1x1x1.mdl")
	self:PhysicsInit(SOLID_VPHYSICS)
	self:SetSolid(SOLID_VPHYSICS)
	self:SetMoveType(MOVETYPE_VPHYSICS)

    if phys:IsValid() then
        phys:EnableGravity(false) -- This is required. Since we are creating our own gravity.
        phys:Wake()
    end
end

function ENT:PhysicsUpdate(phys)
    local phys = self:GetPhysicsObject()
    phys:ApplyForceCenter(Vector(0,0,phys:GetMass()*-9.80665))
end


Personal tools
Navigation