Colliders.lua

From Moondust Wiki
Revision as of 03:25, 29 June 2015 by Hoeloe (talk | contribs) (→‎Poly)
Jump to navigation Jump to search

Download Latest Version

Colliders is a library that helps manage custom collisions in LunaLua, making custom behaviour much easier!

It supports native collisions between players, NPCs and animations, as well as custom Box and Circle colliders to be constructed.

Installation

Place the file colliders.lua into the LuaScriptsLib folder.


How to use

To enable the Colliders library for a specific level, add this line to lunadll.lua:

local colliders = loadAPI("colliders");

This will load the Colliders API.

Example

local colliders = loadAPI("colliders");
    
function onLoop()
    for k,v in pairs(npcs()) do
        if (colliders.collide(player, v)) then
            v:kill();
        end
    end
end

This will kill any NPC that touches the player (but may not stop them from killing the player too!)

Documentation

Classes

These are classes contained within this library

Vector

Values

Vector.x The x value of the vector
Vector.y The y value of the vector
Vector.magnitude The magnitude value of the vector

Functions

Vector.dot vector1 vector2
Calculates the dot product between two vectors number Vector

The first vector.

Vector

The second vector.

Vector:dot vector
Calculates the dot product between the vector and another vector number Vector

The other vector.

Vector:normalise
Calculates the normalised vector (can also be spelled "normalize")
Vector.add vector1 vector2
Adds two vectors together (can also use the notation "a + b") Vector Vector

The first vector.

Vector

The second vector.

Vector:add vector
Adds the vector to another vector Vector Vector

The other vector.

Vector.subtract vector1 vector2
Subtracts one vector from another vector (can also use the notation "a - b") Vector Vector

The first vector.

Vector

The second vector.

Vector:subtract vector
Subtracts another vector from the vector Vector Vector

The other vector.

Vector.mul vector scalar
Multiplies the vector by a constant (can also use the notation "v * c") Vector Vector

The vector.

number

The scalar.

Vector:mul scalar
Multiplies the vector by a scalar Vector number

The scalar.


Point

Values

Point.x The x value of the point collider
Point.y The y value of the point collider

Functions

Point:Draw colour
Draws the collider to the screen with the specified RGBA colour. If no colour is supplied, the default will be used. nil number

A 4-byte hexadecimal code supplying the colour to draw with. The default is 0x0099FF99.

Point:Debug boolean
Tags the collider for debug drawing. nil boolean

Tag or untag the collider.

Box

Values

Box.x The x value of the top left of the box collider
Box.y The y value of the top left of the box collider
Box.width The width of the box collider
Box.height The height of the box collider

Functions

Box:Draw colour
Draws the collider to the screen with the specified RGBA colour. If no colour is supplied, the default will be used. nil number

A 4-byte hexadecimal code supplying the colour to draw with. The default is 0xFF000099.

Box:Debug boolean
Tags the collider for debug drawing. nil boolean

Tag or untag the collider.


Circle

Values

Circle.x The x value of the centre of the circle collider
Circle.y The y value of the centre of the circle collider
Circle.radius The radius of the circle collider

Functions

Circle:Draw colour
Draws the collider to the screen with the specified RGBA colour. If no colour is supplied, the default will be used. nil number

A 4-byte hexadecimal code supplying the colour to draw with. The default is 0xFF00FF99.

Circle:Debug boolean
Tags the collider for debug drawing. nil boolean

Tag or untag the collider.


Tri

Values

Tri.x The x value of the triangle collider
Tri.y The y value of the triangle collider
Tri.v The vertex list of the triangle collider

Functions

Tri:Draw colour
Draws the collider to the screen with the specified RGBA colour. If no colour is supplied, the default will be used. nil number

A 4-byte hexadecimal code supplying the colour to draw with. The default is 0x00FF0099.

Tri:Debug boolean
Tags the collider for debug drawing. nil boolean

Tag or untag the collider.

Tri:Rotate angle
Rotates the collider clockwise by the specified number of degrees. nil number

Angle to rotate in degrees.

Tri:Translate x y
Translates the collider by the specified amount with respect to its position. This will change the origin of the triangle for scaling and rotating. nil number

X coordinate to translate by.

number

Y coordinate to translate by.

Tri:Scale factor
Scales the collider by the specified scale factor. nil number

Scale factor to resize the collider by.


Poly

Values

Poly.x The x value of the polygon collider
Poly.y The y value of the polygon collider
Poly.tris The triangle list of the polygon collider, generated from a vertex list

Functions

Poly:Draw colour
Draws the collider to the screen with the specified RGBA colour. If no colour is supplied, the default will be used. nil number

A 4-byte hexadecimal code supplying the colour to draw with. The default is 0x0000FF99.

Poly:Debug boolean
Tags the collider for debug drawing. nil boolean

Tag or untag the collider.

Poly:Rotate angle
Rotates the collider clockwise by the specified number of degrees. nil number

Angle to rotate in degrees.

Poly:Translate x y
Translates the collider by the specified amount with respect to its position. This will change the origin of the polygon for scaling and rotating. nil number

X coordinate to translate by.

number

Y coordinate to translate by.

Poly:Scale factor
Scales the collider by the specified scale factor. nil number

Scale factor to resize the collider by.

External Use Functions

These are functions you will need to use the library

Point x y
Creates a Point collider. Point number

The X coordinate of the collider.

number

The Y coordinate of the collider.

Box x y width height
Creates a Box collider. Box number

The X coordinate of the top-left corner of the collider.

number

The Y coordinate of the top-left corner of the collider.

number

The width of the collider.

number

The height of the collider.

Circle x y radius
Creates a Circle collider. Circle number

The X coordinate of the centre of the collider.

number

The Y coordinate of the centre of the collider.

number

The radius of the collider.

Tri x y point1 point2 point3
Creates a Triangle collider. Tri number

The X coordinate of the collider.

number

The Y coordinate of the collider.

pair(number)

The coordinate of the first vertex.

pair(number)

The coordinate of the second vertex.

pair(number)

The coordinate of the third vertex.

Poly x y ...
Creates a Polygon collider. Will throw an error if two edges cross over each other. Poly number

The X coordinate of the collider.

number

The Y coordinate of the collider.

some pair(number)s

A variable number of arguments, used to define the vertices of the polygon.

getAABB aabb
Gets the Box collider defining the Axis-Aligned Bounding Box for the given object. Box NPC/Player/Animation/Point/Box/Circle/Tri/Poly

The object to get the collider for. Box objects will return themselves.

getHitbox object
Gets the primitive collider associated with the given object. Point/Box/Circle/Tri/Poly NPC/Player/Animation/Block/Point/Box/Circle/Tri/Poly

The object to get the collider for. Primitive colliders will return themselves.

getSpeedHitbox object
Gets the Box or Circle collider associated with the given object, taking into account its movement speed. Point/Box/Circle/Tri/Poly NPC/Player/Animation/Block/Point/Box/Circle/Tri/Poly

The object to get the collider for. Box and Circle objects will return themselves with no speed modifier.

collide object1 object2
Determines if two objects are touching. boolean NPC/Player/Animation/Block/Point/Box/Circle/Tri/Poly

The first object to test against.

NPC/Player/Animation/Block/Point/Box/Circle/Tri/Poly

The second object to test against.

speedCollide object1 object2
Determines if two objects are touching, taking into account their movement speed. boolean NPC/Player/Animation/Block/Point/Box/Circle/Tri/Poly

The first object to test against. Box and Circle objects will not use a speed modifier.

NPC/Player/Animation/Block/Point/Box/Circle/Tri/Poly

The second object to test against. Box and Circle objects will not use a speed modifier.

bounce object1 object2
Determines if the first object has just bounced on the second object. The second return value will be true if the first object is a spinjumping player. Box and Circle colliders cannot be used as the first object. boolean,boolean NPC/Player/Animation/Block

The first object to test against.

NPC/Player/Animation/Block/Point/Box/Circle/Tri/Poly

The second object to test against.

bounceResponse object height
Causes the object to "bounce" upwards as if they had just jumped on an enemy. nil NPC/Player/Animation/Block

The object to bounce

number:optional

An optional bounce height modifier. By default, this will be the value of the jumpheightBounce() function.

slash player object
Determines if the given player has performed a sword slash and hit the object. Will return false if the player is not Link. boolean Player

The player to test the slash.

NPC/Player/Animation/Block/Point/Box/Circle/Tri/Poly

The object to test against.

downSlash player object
Determines if the given player has performed a downward sword slash and hit the object. Will return false if the player is not Link. boolean Player

The player to test the downward slash.

NPC/Player/Animation/Block/Point/Box/Circle/Tri/Poly

The object to test against.

tail player object
Determines if the given player has performed a tanooki tail spin and hit the object. Will return false if the player is not wearing a tanooki or racoon suit. boolean Player

The player to test the tail spin.

NPC/Player/Animation/Block/Point/Box/Circle/Tri/Poly

The object to test against.

tongue player object
Determines if the given player has performed a Yoshi tongue attack and hit the object. Will return false if the player is not riding a Yoshi. boolean Player

The player to test the tongue.

NPC/Player/Animation/Block/Point/Box/Circle/Tri/Poly

The object to test against.

collideNPC object1 object2
Determines if two objects are colliding. Numbers can be substituted for objects, which will test against all NPCs with the given ID. Returns the number of detected collisions as a second object. boolean,number NPC/Player/Animation/Block/Point/Box/Circle/Tri/Poly/number

The object or NPC ID to test with.

NPC/Player/Animation/Block/Point/Box/Circle/Tri/Poly/number

The object or NPC ID to test against.

collideBlock object1 object2
Determines if two objects are colliding. Numbers can be substituted for objects, which will test against all Blocks with the given ID. Returns the number of detected collisions as a second object. boolean,number NPC/Player/Animation/Block/Point/Box/Circle/Tri/Poly/number

The object or Block ID to test with.

NPC/Player/Animation/Block/Point/Box/Circle/Tri/Poly/number

The object or Block ID to test against.

collideNPCBlock object1 object2
Determines if two objects are colliding. Numbers can be substituted for objects, which will test against all NPCs with the given ID for the first argument, or all Blocks with the given ID for the second argument. Returns the number of detected collisions as a second object. boolean,number NPC/Player/Animation/Block/Point/Box/Circle/Tri/Poly/number

The object or NPC ID to test with.

NPC/Player/Animation/Block/Point/Box/Circle/Tri/Poly/number

The object or Block ID to test against.