CreateColPolygon: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
(8 intermediate revisions by 5 users not shown)
Line 2: Line 2:
{{Server client function}}
{{Server client function}}
{{Note_box|For this function to work correctly, get/set your bound points in an anti-clockwise fashion.}}
{{Note_box|For this function to work correctly, get/set your bound points in an anti-clockwise fashion.}}
This function creates a collision polygon. See [http://en.wikipedia.org/wiki/Polygon 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 somewhere within the polygon. It should be noted this shape is '''2D'''. There should be at least 3 bound points set.  
This function creates a collision polygon. See [http://en.wikipedia.org/wiki/Polygon 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.  
{{VisualizeColshape}}
{{VisualizeColshape}}
==Syntax==  
==Syntax==  
Line 8: Line 8:
colshape createColPolygon ( float fX, float fY, float fX1, float fY1, float fX2, float fY2, float fX3, float fY3, ... )
colshape createColPolygon ( float fX, float fY, float fX1, float fY1, float fX2, float fY2, float fX3, float fY3, ... )
</syntaxhighlight>  
</syntaxhighlight>  
 
{{OOP| |ColShape.Polygon||}}
===Required Arguments===  
===Required Arguments===  
*'''fX:''' The X position of the collision polygon's position - the position that will be returned from [getElementPosition].
*'''fX:''' The X position of the collision polygon's position - the position that will be returned from [[getElementPosition]].
*'''fY:''' The Y position of the collision polygon's position - the position that will be returned from [getElementPosition].
*'''fY:''' The Y position of the collision polygon's position - the position that will be returned from [[getElementPosition]].
*'''fX1:''' The 1st X position of the collision polygon's bound point
*'''fX1:''' The 1st X position of the collision polygon's bound point
*'''fY1:''' The 1st Y position of the collision polygon's bound point
*'''fY1:''' The 1st Y position of the collision polygon's bound point
Line 25: Line 25:
==Example==  
==Example==  
<section name="Server" class="server" show="true">
<section name="Server" class="server" show="true">
This example displays a chat message when a player enters the colshape and allows the colshape to be created using a console function ''set_zone''.
This example displays a chat message when any element hits the colshape and allows the colshape to be created using a console function ''set_zone''.
<syntaxhighlight lang="lua">
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 )
</syntaxhighlight>
</section>
<section name="Server" class="server" show="hide">
This example displays a chat message when any element hits the colshape and allows the colshape to be created using a console function ''set_wall''.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
theZone = false
local theWall = false
 
function shapeHit ( thePlayer )  
function shapeHit( element )
     outputChatBox ( getPlayerName ( thePlayer ) .. " is in the zone!" )               -- display a message in everyone's chat box
     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
end
function setZone ( playerSource, commandName, fX, fY, fX1, fY1, fX2, fY2, fX3, fY3 )  --Remember that after the 3rd position you
                                                                                      --can have as many points as you require to
                                                                                      --create the colshape.


    if ( fY and fX and fX1 and fY1 and fX2 and fX3 and fY3 ) then                      -- check we've got the 8 args we need
function createWallCommandHandler( playerSource, commandName, fromX, fromY, toX, toY )
        local tempCol = createColPolygon ( fX, fY, fX1, fY1, fX2, fY2, fX3, fY3 )     -- create a col
    -- Verify the player has given us the minimum amount of bound points
        if ( tempCol == false ) then                                                   -- did the col get created successfully?
    if not ( fromX and fromY and  toX and toY ) then
            outputConsole ("Syntax is: set_zone <X> <Y> <X1> <Y1> <X2> <Y2> <X3> <Y3>")-- inform the user what the valid syntax is
        return outputChatBox( "Syntax: set_wall <fromX> <fromY> <toX> <toY>", playerSource, 255, 100, 100 )
        else
            if ( theZone ~= false ) then                                              -- did we already have a zone?
                destroyElement ( theZone )                                            -- if so, destroy it
            else
                  addEventHandler ( "onColShapeHit", theZone, shapeHit )              -- add a handler for the onColShapeHit event
            end
            theZone = tempCol                                                          -- and store the new zone we've made
            outputChatBox ( "Zone has moved!" )                                       -- and tell everyone
        end
     end
     end
    -- Calculate the 90° angle for the line between the two coordinates from the arguments
    local radian90DegreeAngle = math.atan2(toY - fromY, toX - fromX) + math.pi / 2
    -- Depth/width of the wall
    local depth = 1
    -- Pre-calculate the cosinus/sinus distances
    local cosinusDepth = math.cos(radian90DegreeAngle) * depth / 2
    local sinusDepth = math.sin(radian90DegreeAngle) * depth / 2
    -- Calculate the points in the game world
    local x1, y1 = fromX - cosinusDepth, fromY - sinusDepth
    local x2, y2 = toX - cosinusDepth, toY - sinusDepth
    local x3, y3 = toX + cosinusDepth, toY + sinusDepth
    local x4, y4 = fromX + cosinusDepth, fromY + sinusDepth
    -- Create the collision shape with the calculated numbers
    local tempCol = createColPolygon( fromX + (toX - fromX) / 2, fromY + (toY - fromY) / 2, x1, y1, x2, y2, x3, y3, x4, y4 )
    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( theWall ) then
        destroyElement( theWall )
    end
    -- Use a variable out-of-scope to keep track of the most recently created collision shape element
    theWall = 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
end
addCommandHandler ( "set_zone", setZone )         -- add a console function called set_zone that will trigger the function setZone
addCommandHandler( "set_wall", createWallCommandHandler )
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>
==Issues==
{{Issues|
{{Issue|9742|[Fixed in 1.5.5-3.12343] Passing this function [[Vector2|Vector2s]] are not fully supported}}
}}


==See Also==
==See Also==
{{Collision shape functions}}
{{Collision shape functions}}
[[hu:createColPolygon]]

Revision as of 17:50, 9 October 2018

This template is no longer in use as it results in poor readability. 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.

[[{{{image}}}|link=|]] Tip: To visualize a colshape when writing scripts, use the client console command showcol

Syntax

colshape createColPolygon ( float fX, float fY, float fX1, float fY1, float fX2, float fY2, float fX3, float fY3, ... )

OOP Syntax Help! I don't understand this!

Method: ColShape.Polygon(...)


Required Arguments

  • fX: The X position of the collision polygon's position - the position that will be returned from getElementPosition.
  • fY: The Y position of the collision polygon's position - the position that will be returned from getElementPosition.
  • fX1: The 1st X position of the collision polygon's bound point
  • fY1: The 1st Y position of the collision polygon's bound point
  • fX2: The 2nd X position of the collision polygon's bound point
  • fY2: The 2nd Y position of the collision polygon's bound point
  • fX3: The 3rd X position of the collision polygon's bound point
  • fY3: The 3rd Y position of the collision polygon's bound point
  • ... From the 3rd position you can have as many points as you require to create the colshape.

Returns

Returns a colshape element if successful, false if invalid arguments were passed to the function.

Example

Click to collapse [-]
Server

This example displays a chat message when any element hits the colshape and allows the colshape to be created using a console function set_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 )
Click to expand [+]
Server

Issues

Issue ID Description
#9742 [Fixed in 1.5.5-3.12343] Passing this function Vector2s are not fully supported

See Also