CollisionSimulator

This provides several collision detection functions.

# Methods

[{"index":"Component Overlap(CollisionGroup collisionGroup, Shape shape)","0":"Finds and returns the first component that overlaps the input shape."}]

[{"index":"Component Overlap(string collisionGroupName, Shape shape)","0":"Finds and returns the first component that overlaps the input shape."}]

[{"index":"Component Overlap(table groups, Shape shape)","0":"Finds and returns the first component that overlaps the entered shape. The groups table is a sequential table of elements from CollisionGroups."}]

[{"index":"table OverlapAll(CollisionGroup collisionGroup, Shape shape)","0":"Finds and returns all components that overlap the input shape."}]

[{"index":"table OverlapAll(string collisionGroupName, Shape shape)","0":"Finds and returns all components that overlap the input shape."}]

[{"index":"table OverlapAll(table groups, Shape shape)","0":"Finds and returns all components that overlap the entered shape. The groups table is a sequential table of elements from CollisionGroups."}]

[{"index":"int32 OverlapAllFast(CollisionGroup collisionGroup, Shape shape, table outputList)","0":"Finds all components that overlap the input shape and inserts the results into the outputList. Returns the number of results."}]

[{"index":"int32 OverlapAllFast(string collisionGroupName, Shape shape, table outputList)","0":"Finds all components that overlap the input shape and inserts the results into the outputList. Returns the number of results."}]

[{"index":"int32 OverlapAllFast(table groups, Shape shape, table outputList)","0":"Finds all components that overlap the input shape and inserts the results into the outputList. Returns the number of results. The groups table is a sequential table of elements from CollisionGroups."}]

[{"index":"Component OverlapBox(string collisionGroupName, Vector2 position, Vector2 size, float angle)","0":"Finds and returns the first component that overlaps the input rectangular collider."}]

[{"index":"table OverlapBoxAll(string collisionGroupName, Vector2 position, Vector2 size, float angle)","0":"Finds and returns the first component that overlaps the input rectangular collider."}]

[{"index":"int32 OverlapBoxAllFast(string collisionGroupName, Vector2 position, Vector2 size, float angle, table outputList)","0":"Finds all components that overlap the input rectangular collider and inserts the result into the outputList. Returns the number of results."}]

[{"index":"Component OverlapCircle(string collisionGroupName, Vector2 position, float radius)","0":"Finds and returns the first Component overlapping the input circular collider."}]

[{"index":"table OverlapCircleAll(string collisionGroupName, Vector2 position, float radius)","0":"Finds and returns all the Components overlapping the input circular collider."}]

[{"index":"int32 OverlapCircleAllFast(string collisionGroupName, Vector2 position, float radius, table outputList)","0":"Finds all the Components overlapping the input circular collider and inserts the result into outputList. Returns the number of results."}]

[{"index":"table OverlapLineAll(string collisionGroupName, Vector2 point1, Vector2 point2)","0":"Finds and returns all the Components overlapping the input line."}]

[{"index":"int32 OverlapLineAllFast(string collisionGroupName, Vector2 point1, Vector2 point2, table outputList)","0":"Finds all the Components overlapping the input line and inserts the result into outputList. Returns the number of results."}]

[{"index":"Component OverlapPoint(CollisionGroup collisionGroup, Vector2 point)","0":"Finds and returns the first Component overlapping the input point."}]

[{"index":"Component OverlapPoint(string collisionGroupName, Vector2 point)","0":"Finds and returns the first Component overlapping the input point."}]

[{"index":"Component OverlapPoint(table groups, Vector2 point)","0":"Finds and returns the first component that includes the input point. The groups table is a sequential table of elements from CollisionGroups."}]

[{"index":"table OverlapPointAll(CollisionGroup collisionGroup, Vector2 point)","0":"Finds and returns all the Components including the input point."}]

[{"index":"table OverlapPointAll(string collisionGroupName, Vector2 point)","0":"Finds and returns all the Components including the input point."}]

[{"index":"table OverlapPointAll(table groups, Vector2 point)","0":"Finds and returns all components that include the entered point. The groups table is a sequential table of elements from CollisionGroups."}]

[{"index":"int32 OverlapPointAllFast(CollisionGroup collisionGroup, Vector2 point, table outputList)","0":"Finds all the Components overlapping the input point and inserts the results into outputList. Returns the number of results."}]

[{"index":"int32 OverlapPointAllFast(string collisionGroupName, Vector2 point, table outputList)","0":"Finds all the Components overlapping the input point and inserts the results into outputList. Returns the number of results."}]

[{"index":"int32 OverlapPointAllFast(table groups, Vector2 point, table outputList)","0":"Finds all the Components overlapping the input point and inserts the results into outputList. Returns the number of results. groups is a sequential table of elements from CollisionGroups."}]

[{"index":"Component Raycast(CollisionGroup collisionGroup, Vector2 origin, Vector2 direction, float distance = 3.4028235E+38)","0":"Finds and returns the first component colliding with the ray while shooting the ray in the direction of its destination by distance from the origin location."}]

[{"index":"Component Raycast(string collisionGroupName, Vector2 origin, Vector2 direction, float distance = 3.4028235E+38)","0":"Finds and returns the first component colliding with the ray while shooting the ray in the direction of its destination by distance from the origin location."}]

[{"index":"Component Raycast(table groups, Vector2 origin, Vector2 direction, float distance = 3.4028235E+38)","0":"Finds and returns the first component colliding with the ray while shooting the ray in the direction of its destination by distance from the origin location. The groups table is a sequential table of elements from CollisionGroups."}]

# Examples

#### OverlapAll

This is an example that destroys a nearby Entity that has TriggerComponent. The CollisionGroup of TriggerComponent is set to TriggerBox by default.
    
    
    @Component
    script AuraTriggerDestroyer extends Component
        @ExecSpace("ServerOnly")
        method void OnUpdate(number delta)
        	local position = self.Entity.TransformComponent.WorldPosition:ToVector2()
        	local circleShape = CircleShape(position, 3)
        	local simulator = _CollisionService:GetSimulator(self.Entity)
    
        -- 	Detects an Entity with TriggerComponent within a radius of 3.
        	local overlaps = simulator:OverlapAll(CollisionGroups.TriggerBox, circleShape)
    
        	for i, component in pairs(overlaps) do
        		if component.EnableInHierarchy == false then
        			continue
        		end
    
        		if component.Entity ~= self.Entity then
        			component.Entity:Destroy()
        		end
        	end
        end
    end
    

#### OverlapAllFast

This is an example that destroys nearby entities that have TriggerComponent. Using the OverlapAllFast() function, you can reuse the table.
    
    
    @Component
    script OverlapDestroyer extends Component
        property any CircleShape = nil
        property table Overlaps = {}
    
        @ExecSpace("ServerOnly")
        method void OnBeginPlay()
        self.CircleShape = CircleShape(Vector2.zero, 3)
        self.Overlaps = {}
        end
    
        @ExecSpace("ServerOnly")
        method void OnUpdate(number delta)
        local position = self.Entity.TransformComponent.WorldPosition:ToVector2()
        self.CircleShape.Position = position
        local simulator = _CollisionService:GetSimulator(self.Entity)
    
        simulator:OverlapAllFast(CollisionGroups.TriggerBox, self.CircleShape, self.Overlaps)
    
        for i, component in pairs(self.Overlaps) do
        	if component.Entity ~= self.Entity then
        		component.Entity:Destroy()
        	end
        end
        end
    end
    

#### RayCast

This is an example of performing a Raycast() from the center of the entity to the location where the screen was touched and destroying any entities detected.
    
    
    @Component
    script ScreenTouchDestroyer extends Component
        @ExecSpace("ServerOnly")
        method void RaycastAndDestroyEntity(Vector3 endPoint)
        local startPoint = self.Entity.TransformComponent.WorldPosition
        local diff = endPoint - startPoint
        local distance = diff:Magnitude()
        local direction = diff:Normalize():ToVector2()
        local origin = startPoint:ToVector2()
    
        local simulator = _CollisionService:GetSimulator(self.Entity)
        -- If the CollisionGroup of the entity is set to TriggerBox, it can detect itself.
        -- We recommend you set it up with a new CollisionGroup.
        local component = simulator:Raycast(CollisionGroups.TriggerBox, origin, direction, distance)
    
        if component == nil then
        	return
        end
    
        if component.Entity ~= self.Entity then
        	component.Entity:Destroy()
        end
        end
    
        @EventSender("Service", "InputService")
        handler HandleScreenTouchEvent(ScreenTouchEvent event)
        --------------- Native Event Sender Info ----------------
        -- Sender: InputService
        -- Space: Client
        ---------------------------------------------------------
    
        -- Parameters
        local TouchId = event.TouchId
        local TouchPoint = event.TouchPoint
        ---------------------------------------------------------
    
        local endPoint = _UILogic:ScreenToWorldPosition(TouchPoint):ToVector3()
        self:RaycastAndDestroyEntity(endPoint)
        end
    end
    

# SeeAlso

  * TransformComponent
  * ScreenTouchEvent
  * CircleShape
  * CollisionGroup
  * Entity
  * CollisionService
  * InputService