2024-02-25 17:31:48 -06:00
|
|
|
/* On collisions, entities are sent a 'hit' object, which looks like this:
|
2023-10-31 08:31:56 -05:00
|
|
|
var HIT = {
|
|
|
|
normal: "The normal of the collision point.",
|
2023-12-12 08:46:27 -06:00
|
|
|
hit: "The gameobject of the object that collided.",
|
2023-10-31 08:31:56 -05:00
|
|
|
sensor: "Boolean for if the colliding object was a sensor.",
|
|
|
|
velocity: "Velocity of the contact.",
|
|
|
|
pos: "Position in world space of the contact.",
|
|
|
|
depth: "Depth of the contact.",
|
|
|
|
};
|
|
|
|
|
2024-02-25 17:31:48 -06:00
|
|
|
*/
|
|
|
|
|
|
|
|
var physics = {
|
2023-09-07 16:46:35 -05:00
|
|
|
dynamic: 0,
|
|
|
|
kinematic: 1,
|
|
|
|
static: 2,
|
2024-02-25 17:31:48 -06:00
|
|
|
|
2023-12-19 17:28:45 -06:00
|
|
|
pos_query(pos, give) {
|
|
|
|
give ??= 25;
|
|
|
|
return cmd(44, pos, give);
|
|
|
|
},
|
2023-09-07 16:46:35 -05:00
|
|
|
|
|
|
|
/* Returns a list of body ids that a box collides with */
|
2023-12-12 08:46:27 -06:00
|
|
|
box_query(box) { return cmd(52, box.pos, box.wh); },
|
2023-09-07 16:46:35 -05:00
|
|
|
|
|
|
|
box_point_query(box, points) {
|
|
|
|
if (!box || !points)
|
|
|
|
return [];
|
|
|
|
|
|
|
|
return cmd(86, box.pos, box.wh, points, points.length);
|
|
|
|
},
|
2023-11-07 12:45:52 -06:00
|
|
|
|
2023-12-12 08:46:27 -06:00
|
|
|
shape_query(shape) { return cmd(80,shape); },
|
2023-11-29 12:40:13 -06:00
|
|
|
|
|
|
|
com(pos) {
|
2023-12-12 08:46:27 -06:00
|
|
|
if (!Array.isArray(pos)) return [0,0];
|
|
|
|
return pos.reduce((a,i) => a.add(i)).map(g => g/pos.length);
|
2023-11-29 12:40:13 -06:00
|
|
|
},
|
2023-09-07 16:46:35 -05:00
|
|
|
};
|
|
|
|
|
2023-10-23 08:08:11 -05:00
|
|
|
physics.doc = {};
|
|
|
|
physics.doc.pos_query = "Returns any object colliding with the given point.";
|
|
|
|
physics.doc.box_query = "Returns an array of body ids that collide with a given box.";
|
|
|
|
physics.doc.box_point_query = "Returns the subset of points from a given list that are inside a given box.";
|
|
|
|
|
2024-02-25 17:31:48 -06:00
|
|
|
physics.collision = {
|
2023-09-07 16:46:35 -05:00
|
|
|
types: {},
|
2023-11-07 17:24:26 -06:00
|
|
|
num: 32,
|
2023-09-07 16:46:35 -05:00
|
|
|
set_collide(a, b, x) {
|
|
|
|
this.types[a][b] = x;
|
|
|
|
this.types[b][a] = x;
|
2023-11-07 17:24:26 -06:00
|
|
|
this.sync();
|
2023-09-07 16:46:35 -05:00
|
|
|
},
|
|
|
|
sync() {
|
|
|
|
for (var i = 0; i < this.num; i++)
|
|
|
|
cmd(76,i,this.types[i]);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2024-02-25 17:31:48 -06:00
|
|
|
for (var i = 0; i < physics.collision.num; i++) {
|
|
|
|
physics.collision.types[i] = [];
|
|
|
|
for (var j = 0; j < physics.collision.num; j++)
|
|
|
|
physics.collision.types[i][j] = false;
|
2023-09-07 16:46:35 -05:00
|
|
|
};
|
|
|
|
|
2024-02-25 17:31:48 -06:00
|
|
|
physics.collision.sync();
|
2023-11-07 17:24:26 -06:00
|
|
|
|
2024-02-25 17:31:48 -06:00
|
|
|
physics.warp = {};
|
|
|
|
physics.warp.gravity = function() { return cmd(253); }
|
|
|
|
physics.warp.damp = function() { return cmd(254); }
|
2024-01-14 10:24:31 -06:00
|
|
|
|
2024-02-25 17:31:48 -06:00
|
|
|
physics.gravity = physics.warp.gravity();
|
2024-03-09 14:27:02 -06:00
|
|
|
physics.gravity.mask = [true];
|
|
|
|
physics.gravity.strength = 500;
|
2024-02-25 17:31:48 -06:00
|
|
|
physics.damp = physics.warp.damp();
|
2024-03-09 14:27:02 -06:00
|
|
|
physics.damp.mask = [true];
|
2024-02-27 10:09:15 -06:00
|
|
|
|
|
|
|
return {
|
|
|
|
physics
|
|
|
|
}
|