prosperon/scripts/physics.js

59 lines
1.6 KiB
JavaScript
Raw Normal View History

2024-02-25 17:31:48 -06:00
/* On collisions, entities are sent a 'hit' object, which looks like this:
var HIT = {
normal: "The normal of the collision point.",
2024-03-20 16:48:03 -05:00
obj: "The gameobject of the object that collided.",
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
*/
2024-10-29 12:41:17 -05:00
export function pos_query(pos, start = world, give = 10) {
2024-04-11 17:17:49 -05:00
var ret;
ret = physics.point_query_nearest(pos, 0);
2024-09-26 11:36:09 -05:00
if (ret) return ret.entity;
2024-04-11 17:17:49 -05:00
2024-09-26 11:36:09 -05:00
return game.all_objects(function (o) {
2024-04-11 17:17:49 -05:00
var dist = Vector.length(o.pos.sub(pos));
if (dist <= give) return o;
});
2024-09-26 11:36:09 -05:00
};
2024-03-19 14:39:19 -05:00
2024-10-29 12:41:17 -05:00
export function box_point_query(box, points) {
2024-03-19 14:39:19 -05:00
if (!box || !points) return [];
2024-09-26 11:36:09 -05:00
var bbox = bbox.fromcwh(box.pos, box.wh);
2024-04-01 17:58:29 -05:00
var inside = [];
2024-09-26 11:36:09 -05:00
for (var i in points) if (bbox.pointin(bbox, points[i])) inside.push[i];
2024-04-01 17:58:29 -05:00
return inside;
2024-09-26 11:36:09 -05:00
};
2024-03-19 14:39:19 -05:00
2024-03-18 08:16:25 -05:00
Object.assign(physics, {
dynamic: 0,
kinematic: 1,
static: 2,
2023-11-29 12:40:13 -06:00
com(pos) {
2024-09-26 11:36:09 -05: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
},
2024-03-18 08:16:25 -05:00
});
physics.doc = {};
physics.doc.pos_query = "Returns any object colliding with the given point.";
2024-04-01 17:58:29 -05:00
physics.doc.box_query = "Calls a given function on every shape object in the given bbox.";
physics.doc.box_point_query = "Returns the subset of points from a given list that are inside a given box.";
2024-03-19 14:39:19 -05:00
physics.gravity = physics.make_gravity();
2024-04-12 13:53:00 -05:00
physics.gravity.mask = ~1;
2024-03-09 18:22:06 -06:00
physics.gravity.strength = 500;
2024-03-19 14:39:19 -05:00
physics.damp = physics.make_damp();
2024-04-12 13:53:00 -05:00
physics.damp.mask = ~1;
2024-09-26 11:36:09 -05:00
physics.delta = 1 / 240;
2024-05-29 20:21:19 -05:00
return {
2024-09-26 11:36:09 -05:00
physics,
};