Aego Русский

Polygon Collider 2D

Convex polygon of 3 to 8 points.

component physics.PolygonCollider2D
On this page

Try it

Polygon collider bounce

The outline follows the triangle collider. With gravity off and Restitution 0, the triangle stops when it strikes the floor.

Change Restitution from 0 to 1 to make the same collision bounce. Reset restores the original contact.

Starts the scene; open the example parameters to experiment.
Open in AegoDownload example project
Scene4 nodes
Triangle
Transform
position[0, 3.09]rotation0scale[1, 1]
Polygon Collider 2D
points[]Vec2[[0, -0.8], [0.8, 0.6], [-0.8, 0.6]]restitutionfloat0
Hexagon
Transform
position[1.5, -2]rotation0scale[1, 1]
Polygon Collider 2D
points[]Vec2[[0.6, 0], [0.3, 0.52], [-0.3, 0.52], [-0.6, 0], [-0.3, -0.52], [0.3, -0.52]]
Floor
Transform
position[0, 4]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")orderint0tintColor#4b5563ffsizeVec2[18, 0.6]
Box Collider 2D
sizeVec2[18, 0.6]
Camera
Transform
position[0, 0]rotation0scale[1, 1]
Camera 2D
zoomfloat40clearColorColor#111827ffscaleenumfitVirtualvirtualVec2[960, 540]activebooltrue
Scriptmain.aes
package demo

import (
    "aego/debug"
    "aego/physics"
)

// The outline follows the three actual polygon corners. The fixed-speed
// collision makes a change of restitution visible without changing the shape.
script PolygonImpact {
    func Ready() {
        node.Get<Body2D>().SetVelocity(Vec2{0, 15})
    }

    func Update(dt float) {
        p := node.WorldPosition()
        top := Vec2{p.X, p.Y - 0.8}
        right := Vec2{p.X + 0.8, p.Y + 0.6}
        left := Vec2{p.X - 0.8, p.Y + 0.6}
        color := Color{1, 0.65, 0.1, 1}
        // Several closely spaced strokes keep the exact outline readable in
        // the small embedded Game View as well as the native editor.
        for i := -2; i <= 2; i++ {
            x := float(i) * 0.08
            debug.Line(Vec2{top.X + x, top.Y}, Vec2{right.X + x, right.Y}, color)
            debug.Line(Vec2{right.X + x, right.Y}, Vec2{left.X + x, left.Y}, color)
            debug.Line(Vec2{left.X + x, left.Y}, Vec2{top.X + x, top.Y}, color)
        }
    }
}

Fields

NameTypeDefaultDescription
OffsetVec2(0, 0)

Position of the shape in node space.

Is Triggerboolfalse

Detects overlaps and sends trigger events without pushing bodies apart.

Frictionfloat0.6

Resistance to sliding along other shapes; 0 is ice. Two shapes use the square root of the product.

≥ 0
Restitutionfloat0

Bounciness: 0 stops on impact, 1 bounces back at the same speed. Two shapes use the larger value.

≥ 0
Densityfloat1

Mass per square unit; gives the body its mass when Mass Mode is auto.

≥ 0
Points[]Vec2[4 items]

Corners in node space, 3 to 8, forming a convex shape; edit them in the Scene View.

read-only in scripts

From a script

Points is read-only in scripts; the shape is edited in the Inspector. Scripts change the surface instead.

package game

script Ice {
    func Ready() {
        poly := node.Get<PolygonCollider2D>()
        poly.Friction = 0.02
        poly.Restitution = 0
    }
}

Script events

Scripts on a node with this component receive these events from the Physics 2D module.