Set object collision box||doc136870

September 20, 2022

Format

Object:setBoundingVolume(boxTable)

Description

Set the object collision box, corresponding to the editor here.

image.png

It is recommended to set double-end synchronization, otherwise there may be a problem of abnormal collision

Parameter

Parameter name Type Description
boxTable table Collider information

Code example

Since there are several types of colliders, the parameters of each type will be somewhat different, here we list them

--Cuboid type
local boxTable = {
  collider = {
    type = "Box", --type
    offset = { -- position
      x = 0,
      y = 0,
      z = 0
    },
    rotation = { -- rotation
      x = 0,
      y = 0,
      z = 0
    },
    extent = { -- volume
      x = 0.6,
      y = 1.8,
      z = 0.6
    },
  },
}

--Capsule body type
local boxTable = {
  collider = {
    type = "Capsule", --type
    offset = { -- position
      x = 0,
      y = 0,
      z = 0
    },
    rotation = { -- rotation
      x = 0,
      y = 0,
      z = 0
    },
    radius = 0.5, -- radius
    height = 1.7, --height
  },
}

--Cylinder type
local boxTable = {
  collider = {
    type = "Cylinder", --type
    offset = { -- position
      x = 0,
      y = 0,
      z = 0
    },
    rotation = { -- rotation
      x = 0,
      y = 0,
      z = 0
    },
    radius = 0.5, -- radius
    height = 1, --height
  },
}

-- sector type
local boxTable = {
  collider = {
    type = "Sector", --type
    offset = { -- position
      x = 0,
      y = 0,
      z = 0
    },
    rotation = { -- rotation
      x = 0,
      y = 0,
      z = 0
    },
    radius = 0.5, -- radius
    height = 1, --height
    angle = { -- angle
      max = 360,
      min = 0
    },
  },
}

-- sphere type
local boxTable = {
  collider = {
    type = "Sphere", --type
    offset = { -- position
      x = 0,
      y = 0,
      z = 0
    },
    rotation = { -- rotation
      x = 0,
      y = 0,
      z = 0
    },
    radius = 0.5, -- radius
  },
}

In commoncommon script, add a value definition, set the collision box when changing the value definition

Entity.addValueDef("boundingVolume",{}, true, true, true,false,true)        
Entity.addValueFunc("boundingVolume", function(entity, boxTable)    
    entity:setBoundingVolume(boxTable)        
end)

On the server side of the entity templatemount script, register when the entity of this class is generatedcallback function, which changes the collision box of the entity

-- sphere type
local boxTable = {
   collider = {
     type = "Sphere", --type
     offset = { -- position
       x = 0,
       y = 0,
       z = 0
     },
     rotation = { -- rotation
       x = 0,
       y = 0,
       z = 0
     },
     radius = 0.5, -- radius
   },
}

Trigger.RegisterHandler(this:cfg(),"ENTITY_ENTER",function(context)
     local entity = context.obj1
     entity:setValue("boundingVolume",boxTable)
   End)