CreatePhysCollideBox

From Garry's Mod
Jump to: navigation, search
 CreatePhysCollideBox( )

Contents

Description

Creates a new PhysCollide from the given bounds.

BUG

This fails to create planes or points - no components of the mins or maxs can be the same.

Issue Tracker: #3568

Arguments

Vector mins

Min corner of the box. This is not automatically ordered with the maxs and must contain the smallest vector components. See OrderVectors.

Arguments

Vector maxs

Max corner of the box. This is not automatically ordered with the mins and must contain the largest vector components.

Returns

PhysCollide

The new PhysCollide. This will be a NULL PhysCollide (PhysCollide:IsValid returns false) if given bad vectors or no more PhysCollides can be created in the physics engine.

Examples

Example

A box that interacts correctly with VPhysics objects and player movement.

AddCSLuaFile()

DEFINE_BASECLASS( "base_anim" )

ENT.PrintName = "Cube"
ENT.Spawnable = true

ENT.Mins = Vector( -16, -16, -16 )
ENT.Maxs = Vector(  16,  16,  16 )

function ENT:Initialize()
    self.PhysCollide = CreatePhysCollideBox( self.Mins, self.Maxs )
    self:SetCollisionBounds( self.Mins, self.Maxs )

    if SERVER then
        self:PhysicsInitBox( self.Mins, self.Maxs )
        self:SetSolid( SOLID_VPHYSICS )
        self:PhysWake()
    end

    if CLIENT then
        self:SetRenderBounds( self.Mins, self.Maxs )
    end

    self:EnableCustomCollisions( true )
    self:DrawShadow( false )
end

-- Handles collisions against traces. This includes player movement.
function ENT:TestCollision( startpos, delta, isbox, extents )
    if not IsValid( self.PhysCollide ) then
        return
    end

    -- TraceBox expects the trace to begin at the center of the box, but TestCollision is bad
    local max = extents
    local min = -extents
    max.z = max.z - min.z
    min.z = 0

    local hit, norm, frac = self.PhysCollide:TraceBox( self:GetPos(), self:GetAngles(), startpos, startpos + delta, min, max )

    if not hit then
        return
    end

    return { 
        HitPos = hit,
        Normal  = norm,
        Fraction = frac,
    }
end

function ENT:Draw()
    render.DrawWireframeBox( self:GetPos(), self:GetAngles(), self.Mins, self.Maxs, Color( 255, 0, 0 ), true )
end


Personal tools
Navigation