createColPolygon | Multi Theft Auto: Wiki Skip to content

createColPolygon

Client-side
Server-side
Shared

This page is incomplete! Help wanted!

Please finish this page using the corresponding Old Wiki article.
Go to Contribution guidelines for more information.


This function creates a collision polygon. See Wikipedia for a definition of a polygon. The first set of X Y of this shape is not part of the colshape bounds, so can set anywhere in the game world, however for performance, place it as close to the centre of the polygon as you can. It should be noted this shape is 2D. There should be at least 3 bound points set.

Note

For this function to work correctly, get/set your bound points in an anti-clockwise fashion.

Note

Even though this is a 2D colshape, isElementWithinColShape returns false if an element is in the area but below 0 Z height.

Tip

To visualize a colshape when writing scripts, use the client console command showcol

Syntax

createColPolygon ( )

Code Examples

server

This example displays a chat message when any element hits the colshape and allows the colshape to be created using a console functionset_zone.

local theZone = false
function shapeHit( element )
local descriptor = "undefined"
if getElementType( element ) == "player" then
-- Use the player's name
descriptor = getPlayerName( element )
elseif getElementType( element ) == "vehicle" then
-- Use the vehicle's model name
descriptor = getVehicleName( element ) or descriptor
end
-- Output a message in the chat box for everyone on the server
outputChatBox( descriptor .. " is in the zone!" )
end
function createZoneCommandHandler( playerSource, commandName, fX, fY, fX1, fY1, fX2, fY2, fX3, fY3, ... )
-- Verify the player has given us the minimum amount of bound points
if not ( fY and fX and fX1 and fY1 and fX2 and fX3 and fY3 ) then
return outputChatBox( "Syntax: set_zone <X> <Y> <X1> <Y1> <X2> <Y2> <X3> <Y3> [<Xn> <Yn> ...]", playerSource, 255, 100, 100 )
end
-- Create the collision shape with the numbers from the arguments
local tempCol = createColPolygon( fX, fY, fX1, fY1, fX2, fY2, fX3, fY3, ... )
if not tempCol then
return outputChatBox( "Error: Couldn't create collision polygon", playerSource, 255, 100, 100 )
end
-- Destroy the previous collision polygon shape in case we already have one
if isElement( theZone ) then
destroyElement( theZone )
end
-- Use a variable out-of-scope to keep track of the most recently created collision shape element
theZone = tempCol
-- Attach an event handler to the element to get notified whenever an element hits our collision shape
addEventHandler( "onColShapeHit", tempCol, shapeHit, false )
outputChatBox( "Success: Collision shape has been created!", playerSource, 100, 255, 100 )
end
addCommandHandler( "set_zone", createZoneCommandHandler )