Layers¶
Layers are used in physics to selectively enable or disable collisions between entities.
In YAHAHA, layers are accessed via scripts, which allow developers to set and edit layers, assign entities to specific layers, and adjust collisions to control which layers can interact with each other. By default, all entities are on layer 1 and the layer can be changed between 1 and 31.
SetPhysicsLayer¶
To set a layer for an entity using SetPhysicsLayer:
local userDefinedLayer_11 = 11
local entity_A = YaEntity.New(8)
PhysicsAPI.SetPhysicsLayer(entity_A, userDefinedLayer_11)
CollisionGroupSetCollidable¶
To place entities on layers that will not collide:
local userDefinedLayer_11 = 11
local userDefinedLayer_12 = 12
-- Ignore the collision between layer 11 and layer 12
PhysicsAPI.CollisionGroupSetCollidable(userDefinedLayer_11, userDefinedLayer_12, false)
local entity_A = YaEntity.New(8)
local entity_B = YaEntity.New(9)
-- Set a layer 11 for entity A, and layer 12 for entity B
PhysicsAPI.SetPhysicsLayer(entity_A, userDefinedLayer_11)
PhysicsAPI.SetPhysicsLayer(entity_B, userDefinedLayer_12)
In this example, we set the collision between layers 11 and 12 to be ignored, and then assign entity A to layer 11 and entity B to layer 12. As a result, the two entities from different layers will not collide.
QueryPhysicsLayer¶
To detect entities on a specified layer with raycasting:
local userDefinedLayer_11 = 11
-- Detect any entity on layer 11 using a ray:
local queryInstance = YaQueryParameter.Instance()
local queryParam = queryInstance:QueryPhysicsLayer(userDefinedLayer_11)
local queryResult = PhysicsAPI.RaycastSingle(float3.New(0, 10, 0), float3.New(0, 1, 0), queryParam)
print(queryResult.Entity.EntityId)
In this example, we use the ray to detect entities on layer 11 in an upward direction and expect the ID of the first entity that it hits to be returned.