Add enabling and disabling colliders; fix bug related to it

This commit is contained in:
John Alanbrook 2023-10-31 13:31:56 +00:00
parent 92ccb72c04
commit ecd31eeafa
5 changed files with 37 additions and 25 deletions

View file

@ -54,11 +54,10 @@ Object.rkeys = function(o)
return keys; return keys;
} }
Object.extend = function(from, src) Object.extend = function(from)
{ {
var n = {}; var n = {};
Object.mixin(n, from); Object.mixin(n, from);
Object.mixin(n, src);
return n; return n;
} }
@ -69,6 +68,15 @@ Object.mixin = function(target, source)
return target; return target;
}; };
Object.mix = function(...objs)
{
var n = {};
for (var o of objs)
Object.mixin(n,o);
return n;
}
Object.deepmixin = function(target, source) Object.deepmixin = function(target, source)
{ {
var o = source; var o = source;

View file

@ -1,7 +1,7 @@
function assign_impl(obj, impl) function assign_impl(obj, impl)
{ {
var tmp = {}; var tmp = {};
for (var key in impl) for (var key of Object.keys(impl))
if (typeof obj[key] !== 'undefined' && typeof obj[key] !== 'function') if (typeof obj[key] !== 'undefined' && typeof obj[key] !== 'function')
tmp[key] = obj[key]; tmp[key] = obj[key];
@ -337,12 +337,13 @@ var collider2d = Object.copy(component, {
impl: { impl: {
set sensor(x) { cmd(18,this.shape,x); }, set sensor(x) { cmd(18,this.shape,x); },
get sensor() { return cmd(21,this.shape); }, get sensor() { return cmd(21,this.shape); },
// set enabled(x) { cmd(22,this.shape,x); }, set enabled(x) { cmd(22,this.shape,x); },
// get enabled() { return cmd(23,this.shape); } get enabled() { return cmd(23,this.shape); }
}, },
}); });
Object.hide(collider2d.impl, 'enabled');
collider2d.inputs = {}; collider2d.inputs = {};
collider2d.inputs['M-s'] = function() { this.sensor = !this.sensor; } collider2d.inputs['M-s'] = function() { this.sensor = !this.sensor; }
collider2d.inputs['M-s'].doc = "Toggle if this collider is a sensor."; collider2d.inputs['M-s'].doc = "Toggle if this collider is a sensor.";
@ -415,17 +416,14 @@ component.polygon2d = Object.copy(collider2d, {
}, },
}); });
component.polygon2d.impl = { component.polygon2d.impl = Object.mix(collider2d.impl, {
set sensor(x) { cmd(18,this.shape,x); },
get sensor() { return cmd(21,this.shape); },
sync() { sync() {
cmd_poly2d(0, this.id, this.spoints); cmd_poly2d(0, this.id, this.spoints);
}, },
query() { query() {
return cmd(80, this.shape); return cmd(80, this.shape);
}, },
}; });
var polygon2d = component.polygon2d; var polygon2d = component.polygon2d;
@ -585,10 +583,7 @@ component.edge2d = Object.copy(collider2d, {
}, },
}); });
component.edge2d.impl = { component.edge2d.impl = Object.mix({
set sensor(x) { cmd(18,this.shape,x); },
get sensor() { return cmd(21,this.shape); },
set thickness(x) { set thickness(x) {
cmd_edge2d(1,this.id,x); cmd_edge2d(1,this.id,x);
}, },
@ -600,7 +595,7 @@ component.edge2d.impl = {
cmd_edge2d(0,this.id,points); cmd_edge2d(0,this.id,points);
this.sensor = sensor; this.sensor = sensor;
}, },
}; }, component.edge2d.impl);
var bucket = component.edge2d; var bucket = component.edge2d;
bucket.spoints.doc = "Returns the controls points after modifiers are applied, such as it being hollow or mirrored on its axises."; bucket.spoints.doc = "Returns the controls points after modifiers are applied, such as it being hollow or mirrored on its axises.";
@ -728,10 +723,7 @@ component.circle2d = Object.copy(collider2d, {
_enghook: make_circle2d, _enghook: make_circle2d,
}); });
component.circle2d.impl = { component.circle2d.impl = Object.mix({
set sensor(x) { cmd(18,this.shape,x); },
get sensor() { return cmd(21,this.shape); },
set radius(x) { cmd_circle2d(0,this.id,x); }, set radius(x) { cmd_circle2d(0,this.id,x); },
get radius() { return cmd_circle2d(2,this.id); }, get radius() { return cmd_circle2d(2,this.id); },
@ -740,7 +732,7 @@ component.circle2d.impl = {
set offset(x) { cmd_circle2d(1,this.id,x); }, set offset(x) { cmd_circle2d(1,this.id,x); },
get offset() { return cmd_circle2d(3,this.id); }, get offset() { return cmd_circle2d(3,this.id); },
}; }, collider2d.impl);;
/* ASSETS */ /* ASSETS */

View file

@ -1,3 +1,15 @@
/* On collisions, entities are sent a 'hit' object, which looks like this: */
var HIT = {
normal: "The normal of the collision point.",
hit: "The gameobject ID 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.",
id: "Gameobject ID of the colliding object.",
obj: "Entity that collided."
};
var Physics = { var Physics = {
dynamic: 0, dynamic: 0,
kinematic: 1, kinematic: 1,

View file

@ -562,9 +562,9 @@ void phys2d_dbgdrawedge(struct phys2d_edge *edge) {
/************ COLLIDER ****************/ /************ COLLIDER ****************/
void shape_enabled(struct phys2d_shape *shape, int enabled) { void shape_enabled(struct phys2d_shape *shape, int enabled) {
if (enabled) if (enabled)
cpShapeSetFilter(shape->data, CP_SHAPE_FILTER_ALL); cpShapeSetFilter(shape->shape, CP_SHAPE_FILTER_ALL);
else else
cpShapeSetFilter(shape->data, CP_SHAPE_FILTER_NONE); cpShapeSetFilter(shape->shape, CP_SHAPE_FILTER_NONE);
} }
int shape_is_enabled(struct phys2d_shape *shape) { int shape_is_enabled(struct phys2d_shape *shape) {
@ -588,7 +588,7 @@ int shape_get_sensor(struct phys2d_shape *shape) {
if (!shape->shape) { if (!shape->shape) {
struct phys2d_edge *edge = shape->data; struct phys2d_edge *edge = shape->data;
if (arrlen(edge->shapes) > 0) return cpShapeGetSensor(edge->shapes[0]); if (arrlen(edge->shapes) > 0) return cpShapeGetSensor(edge->shapes[0]);
YughError("Attempted to get the sensor of an edge with no shapes. It has %d points.", arrlen(edge->points)); YughInfo("Attempted to get the sensor of an edge with no shapes. It has %d points.", arrlen(edge->points));
return 0; return 0;
} }

View file

@ -24,7 +24,7 @@ extern struct rgba sleep_color;
struct phys2d_shape { struct phys2d_shape {
cpShape *shape; cpShape *shape;
int go; int go;
void *data; void *data; /* The specific subtype; phys2d_circle, etc */
void (*debugdraw)(void *data); void (*debugdraw)(void *data);
float (*moi)(void *data, float mass); float (*moi)(void *data, float mass);
void (*apply)(void *data); void (*apply)(void *data);