physics fixes

This commit is contained in:
John Alanbrook 2023-05-27 15:13:20 +00:00
parent a8f7f20d6e
commit 591f48c703
12 changed files with 117 additions and 44 deletions

View file

@ -13,6 +13,11 @@ QFLAGS :=
ifdef DBG
QFLAGS += -O0 -g -DDBG
INFO = dbg
ifeq ($(CC),tcc)
QFLAGS +=
endif
else
QFLAGS += -O2
INFO = rel

View file

@ -173,6 +173,7 @@ void phys2d_set_gravity(cpVect v) {
void phys2d_update(float deltaT) {
cpSpaceStep(space, deltaT);
flush_collide_cbs();
}
void init_phys2dshape(struct phys2d_shape *shape, int go, void *data) {
@ -262,9 +263,9 @@ void phys2d_dbgdrawcpcirc(cpCircleShape *c) {
float radius = cpCircleShapeGetRadius(c);
struct rgba color = shape_color(c);
float seglen = cpShapeGetSensor(c) ? 5 : -1;
draw_circle(pos.x, pos.y, radius, 1, color, seglen);
draw_circle(pos, radius, 1, color, seglen);
color.a = col_alpha;
draw_circle(pos.x,pos.y,radius,radius,color,-1);
draw_circle(pos,radius,radius,color,-1);
}
void phys2d_dbgdrawcircle(struct phys2d_circle *circle) {
@ -586,6 +587,21 @@ void phys2d_reindex_body(cpBody *body) {
cpSpaceReindexShapesForBody(space, body);
}
struct postphys_cb {
struct callee c;
JSValue send;
};
static struct postphys_cb begins[512];
static uint32_t bptr;
void flush_collide_cbs() {
for (int i = 0; i < bptr; i++)
script_callee(begins[i].c, 1, &begins[i].send);
bptr = 0;
}
void duk_call_phys_cb(cpVect norm, struct callee c, int hit, cpArbiter *arb) {
cpShape *shape1;
cpShape *shape2;
@ -599,7 +615,11 @@ void duk_call_phys_cb(cpVect norm, struct callee c, int hit, cpArbiter *arb) {
JS_SetPropertyStr(js, obj, "pos", vec2js(cpArbiterGetPointA(arb, 0)));
JS_SetPropertyStr(js, obj, "id", JS_NewInt32(js,hit));
JS_SetPropertyStr(js,obj,"obj", JS_DupValue(js,id2go(hit)->ref));
script_callee(c, 1, &obj);
begins[bptr].c = c;
begins[bptr].send = obj;
bptr++;
return;
}
#define CTYPE_BEGIN 0

View file

@ -138,6 +138,8 @@ void phys2d_setup_handlers(int go);
int *phys2d_query_shape(struct phys2d_shape *shape);
int *phys2d_query_box_points(cpVect pos, cpVect wh, cpVect *points, int n);
void flush_collide_cbs();
void phys2d_reindex_body(cpBody *body);
cpVect world2go(struct gameobject *go, cpVect worldpos);
cpVect go2world(struct gameobject *go, cpVect gopos);

View file

@ -70,7 +70,7 @@ static sg_bindings circle_bind;
static sg_shader csg;
static int circle_count = 0;
struct circle_vertex {
float pos[2];
cpVect pos;
float radius;
struct rgba color;
float segsize;
@ -507,11 +507,10 @@ void draw_edge(cpVect *points, int n, struct rgba color, int thickness, int clos
}
}
void draw_circle(int x, int y, float radius, float pixels, struct rgba color, float seg)
void draw_circle(cpVect pos, float radius, float pixels, struct rgba color, float seg)
{
struct circle_vertex cv;
cv.pos[0] = x;
cv.pos[1] = y;
cv.pos = pos;
cv.radius = radius;
cv.color = color;
cv.segsize = seg/radius;

View file

@ -13,8 +13,8 @@ void draw_arrow(struct cpVect start, struct cpVect end, struct rgba, int capsize
void draw_edge(struct cpVect *points, int n, struct rgba color, int thickness, int closed, int flags, struct rgba line_color, float line_seg);
/* pixels - how many pixels thick, segsize - dashed line seg len */
void draw_circle(int x, int y, float radius, float pixels, struct rgba color, float seg);
void draw_box(struct cpVect c, struct cpVect wh, struct rgba color);
void draw_circle(cpVect c, float radius, float pixels, struct rgba color, float seg);
void draw_box(cpVect c, cpVect wh, struct rgba color);
void draw_poly(cpVect *points, int n, struct rgba color);
void draw_grid(int width, int span, struct rgba color);

View file

@ -19,9 +19,8 @@ int logLevel = 1;
3 critical
*/
//char *logstr[] = { "INFO", "WARN", "\x1b[1;31mERROR\x1b[0m", "CRITICAL" };
char *logstr[] = { "info", "warn", "error", "critical" };
char *catstr[] = {"engine", "script"};
char *catstr[] = {"engine", "script", "render"};
FILE *logfile = NULL;
@ -48,12 +47,12 @@ void mYughLog(int category, int priority, int line, const char *file, const char
va_end(args);
char buffer[ERROR_BUFFER] = { '\0' };
snprintf(buffer, ERROR_BUFFER, "%g | %s:%d: %s, %s: %s\n", ticks, file, line, logstr[priority], catstr[category], msgbuffer);
snprintf(buffer, ERROR_BUFFER, "%s:%d: %s, %s: %s\n", file, line, logstr[priority], catstr[category], msgbuffer);
log_print(buffer);
if (category == 1 && priority >= 2)
js_stacktrace();
// if (category != LOG_SCRIPT && priority >= 2)
// js_stacktrace();
}
#endif
}

View file

@ -10,6 +10,10 @@
#define LOG_ERROR 2
#define LOG_CRITICAL 3
#define LOG_ENGINE 0
#define LOG_SCRIPT 1
#define LOG_RENDER 2
#define M_PI 3.14
extern char lastlog[];

View file

@ -1017,6 +1017,10 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
case 114:
return bool2js(js2sprite(argv[1])->enabled);
case 115:
draw_circle(js2vec2(argv[1]), js2number(argv[2]), js2number(argv[2]), js2color(argv[3]), -1);
break;
}
if (str)

View file

@ -98,7 +98,7 @@ int js_print_exception(JSValue v) {
const char *name = JS_ToCString(js, JS_GetPropertyStr(js, exception, "name"));
const char *msg = JS_ToCString(js, JS_GetPropertyStr(js, exception, "message"));
const char *stack = JS_ToCString(js, val);
YughWarn("%s :: %s\n%s", name, msg,stack);
YughLog(LOG_SCRIPT, LOG_ERROR, "%s :: %s\n%s", name, msg,stack);
JS_FreeCString(js, name);
JS_FreeCString(js, msg);

View file

@ -62,16 +62,33 @@ int fps;
#define SIM_PAUSE 2
#define SIM_STEP 3
#ifdef __TINYC__
int backtrace(void **buffer, int size) {
extern uint64_t *__libc_stack_end;
uint64_t **p, *bp, *frame;
asm ("mov %%rbp, %0;" : "=r" (bp));
p = (uint64_t**) bp;
int i = 0;
while (i < size) {
frame = p[0];
if (frame < bp || frame > __libc_stack_end) {
return i;
}
buffer[i++] = p[1];
p = (uint64_t**) frame;
}
return i;
}
#endif
void print_stacktrace() {
void *ents[512];
size_t size;
size = backtrace(ents, 512);
size_t size = backtrace(ents, 512);
YughCritical("====================BACKTRACE====================");
char **stackstr = backtrace_symbols(ents, size);
YughInfo("Stack size is %d.", size);
YughCritical("Stack size is %d.", size);
for (int i = 0; i < size; i++)
YughCritical(stackstr[i]);

View file

@ -6,6 +6,13 @@ var Gizmos = {
},
};
var Shape = {
circle(pos, radius, color) {
cmd(115, pos, radius, color);
},
};
var Debug = {
draw_grid(width, span, color) {
color = color ? color : Color.green;

View file

@ -12,8 +12,6 @@ function load(file) {
files[file] = modtime;
}
load("scripts/base.js");
var Log = {
@ -62,7 +60,6 @@ var Log = {
},
stack(skip = 0) {
Log.warn("Printing stack");
var stack = (new Error()).stack;
var n = stack.next('\n',0)+1;
for (var i = 0; i < skip; i++)
@ -456,18 +453,28 @@ function state2str(state) {
}
var Register = {
inloop: false,
loopcbs: [],
finloop() {
this.loopcbs.forEach(x => x());
this.loopcbs = [];
},
wraploop(loop) {
this.inloop = true;
loop();
this.inloop = false;
this.finloop();
},
updates: [],
update(dt) {
this.updates.forEach(x => x[0].call(x[1], dt));
this.wraploop(() => this.updates.forEach(x => x[0].call(x[1], dt)));
},
physupdates: [],
postphys_cbs: [],
physupdate(dt) {
this.postphys_cbs.forEach(x => x());
this.postphys_cbs = [];
this.physupdates.forEach(x => x[0].call(x[1], dt));
this.wraploop(() => this.physupdates.forEach(x => x[0].call(x[1], dt)));
},
guis: [],
@ -520,6 +527,15 @@ var Register = {
draw() {
this.draws.forEach(x => x[0].call(x[1]));
},
endofloop(fn) {
if (!this.inloop)
fn();
else {
Log.warn("resgieted ...");
this.loopcbs.push(fn);
}
},
};
Register.unregister_obj(null);
@ -1464,9 +1480,8 @@ var gameobject = {
stop() {},
kill() {
Log.warn(`Killing ${this.toString()}`);
Register.endofloop(() => {
cmd(2, this.body);
delete Game.objects[this.body];
if (this.level)
@ -1484,6 +1499,7 @@ var gameobject = {
}
this.stop();
});
},
get up() {