File: collider.md

package info (click to toggle)
surgescript 0.5.4.4-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,876 kB
  • sloc: ansic: 13,674; makefile: 16
file content (137 lines) | stat: -rw-r--r-- 4,011 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
Collider
========

A collider, or collision object, is used to detect collisions. There are different types of colliders, each with a different shape. They are usually centered on the hot spot of sprites, but that can be changed by altering their *anchor*.

Colliders must be spawned as children of [entities](/engine/entity). To detect collisions, you may implement function `onCollision()` on the entity (see the example below), or use the colliders directly. Additionally, a single entity may have multiple colliders attached to it. This allows users to work with more complex shapes than simple primitives.

A collider is an abstract concept, and hence can't be spawned directly. Rather, you can spawn colliders of specific shapes, such as [CollisionBox](/engine/collisionbox) and [CollisionBall](/engine/collisionball). All Colliders share some functionalities (detailed in this page), but there are functionalities tied to specific shapes.

*Example*

```
using SurgeEngine.Actor;
using SurgeEngine.Player;
using SurgeEngine.Collisions.CollisionBall;

object "CollisionDoll" is "entity"
{
    actor = Actor("CollisionDoll");
    collider = CollisionBall(25); // ball with radius = 25px

    // The player has a built-in collider
    // Let's make it visible for debugging
    state "main"
    {
        player = Player.active;
        player.collider.visible = true;
        collider.visible = true;
    }

    // Detect collisions between a collider that is a child
    // of this object and any other collider in the game
    fun onCollision(otherCollider)
    {
        // A collision has occurred.
        Console.print("Collided with something");

        // Collided with a player?
        if(otherCollider.entity.hasTag("player")) {
            player = otherCollider.entity;
            Console.print("Touched " + player.name);
        }
    }

    // While onCollision() catches the moment a collision
    // first occurs, onOverlap() is called every frame
    // this collider collides with other collider
    fun onOverlap(otherCollider)
    {
        // This function is optional.
    }
}
```


Properties
----------

#### entity

`entity`: object, read-only.

The [Entity](/engine/entity) associated with this collider.

#### enabled

`enabled`: boolean.

Is the collider enabled? A collider that is not enabled will not notify the parent object if a collision occurs. The default value is `true`, i.e., colliders are enabled by default.

#### visible

`visible`: boolean.

Is the collider visible? This is useful for debugging. The default value is `false`.

Functions
---------

#### collidesWith

`collidesWith(collider)`

Checks if this collider is colliding with some other collider.

*Arguments*

* `collider`: [Collider](/engine/collider) object. The other collider.

*Returns*

Returns `true` if there is a collision (the colliders overlap), or `false` otherwise.

#### contains

`contains(point)`

Checks if the collider contains the given point, given in world coordinates.

*Arguments*

* `point`: [Vector2](/engine/vector2) object. The point to be tested.

*Returns*

Returns `true` if the point is contained in the collider, or `false` otherwise.

#### setAnchor

`setAnchor(x, y)`

Defines the anchor of the collider to be (`x`, `y`), where these values are (usually) numbers between 0.0 and 1.0. Imagine a bounding box of the collider. Point (0.5, 0.5) is the default, representing its center. Point (0.0, 0.0) is the top-left and (1,0, 1.0), the bottom-right. The anchor of the collider will be aligned to the hot spot of the sprite of the entity.

*Arguments*

* `x`: number. Usually a value between 0.0 and 1.0.
* `y`: number. Usually a value between 0.0 and 1.0.

*Returns*

Returns the collider itself.

*Example*

```
// ...
using SurgeEngine.Collisions.CollisionBox;

object "CollisionTestObject" is "entity"
{
    // see that the following collision box
    // has its anchor on pixel (16, 64)
    collider = CollisionBox(32, 64).setAnchor(0.5, 1.0);

    // ...
}
```