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;
}
Object.extend = function(from, src)
Object.extend = function(from)
{
var n = {};
Object.mixin(n, from);
Object.mixin(n, src);
return n;
}
@ -69,6 +68,15 @@ Object.mixin = function(target, source)
return target;
};
Object.mix = function(...objs)
{
var n = {};
for (var o of objs)
Object.mixin(n,o);
return n;
}
Object.deepmixin = function(target, source)
{
var o = source;

View file

@ -1,7 +1,7 @@
function assign_impl(obj, impl)
{
var tmp = {};
for (var key in impl)
for (var key of Object.keys(impl))
if (typeof obj[key] !== 'undefined' && typeof obj[key] !== 'function')
tmp[key] = obj[key];
@ -337,12 +337,13 @@ var collider2d = Object.copy(component, {
impl: {
set sensor(x) { cmd(18,this.shape,x); },
get sensor() { return cmd(21,this.shape); },
// set enabled(x) { cmd(22,this.shape,x); },
// get enabled() { return cmd(23,this.shape); }
set enabled(x) { cmd(22,this.shape,x); },
get enabled() { return cmd(23,this.shape); }
},
});
Object.hide(collider2d.impl, 'enabled');
collider2d.inputs = {};
collider2d.inputs['M-s'] = function() { this.sensor = !this.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 = {
set sensor(x) { cmd(18,this.shape,x); },
get sensor() { return cmd(21,this.shape); },
component.polygon2d.impl = Object.mix(collider2d.impl, {
sync() {
cmd_poly2d(0, this.id, this.spoints);
},
query() {
return cmd(80, this.shape);
},
};
});
var polygon2d = component.polygon2d;
@ -585,10 +583,7 @@ component.edge2d = Object.copy(collider2d, {
},
});
component.edge2d.impl = {
set sensor(x) { cmd(18,this.shape,x); },
get sensor() { return cmd(21,this.shape); },
component.edge2d.impl = Object.mix({
set thickness(x) {
cmd_edge2d(1,this.id,x);
},
@ -600,7 +595,7 @@ component.edge2d.impl = {
cmd_edge2d(0,this.id,points);
this.sensor = sensor;
},
};
}, component.edge2d.impl);
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.";
@ -728,10 +723,7 @@ component.circle2d = Object.copy(collider2d, {
_enghook: make_circle2d,
});
component.circle2d.impl = {
set sensor(x) { cmd(18,this.shape,x); },
get sensor() { return cmd(21,this.shape); },
component.circle2d.impl = Object.mix({
set radius(x) { cmd_circle2d(0,this.id,x); },
get radius() { return cmd_circle2d(2,this.id); },
@ -740,7 +732,7 @@ component.circle2d.impl = {
set offset(x) { cmd_circle2d(1,this.id,x); },
get offset() { return cmd_circle2d(3,this.id); },
};
}, collider2d.impl);;
/* 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 = {
dynamic: 0,
kinematic: 1,

View file

@ -562,9 +562,9 @@ void phys2d_dbgdrawedge(struct phys2d_edge *edge) {
/************ COLLIDER ****************/
void shape_enabled(struct phys2d_shape *shape, int enabled) {
if (enabled)
cpShapeSetFilter(shape->data, CP_SHAPE_FILTER_ALL);
cpShapeSetFilter(shape->shape, CP_SHAPE_FILTER_ALL);
else
cpShapeSetFilter(shape->data, CP_SHAPE_FILTER_NONE);
cpShapeSetFilter(shape->shape, CP_SHAPE_FILTER_NONE);
}
int shape_is_enabled(struct phys2d_shape *shape) {
@ -588,7 +588,7 @@ int shape_get_sensor(struct phys2d_shape *shape) {
if (!shape->shape) {
struct phys2d_edge *edge = shape->data;
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;
}

View file

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