diff --git a/scripts/base.js b/scripts/base.js index 6007a56..d775e09 100644 --- a/scripts/base.js +++ b/scripts/base.js @@ -955,6 +955,14 @@ value: function(b) { return c; }}); +Object.defineProperty(Array.prototype, 'normalized', { + value: function() { + var c = this.slice(); + var len = Vector.length(c); + return c.map(v => v/len); + } +}); + Object.defineProperty(Array.prototype, 'newfirst', { value: function(i) { var c = this.slice(); @@ -1364,21 +1372,16 @@ Boundingbox.bl = function(bb) { return [bb.l, bb.b] }; /* VECTORS */ var Vector = { - x: 0, - y: 0, length(v) { var sum = v.reduce(function(acc, val) { return acc + val**2; }, 0); return Math.sqrt(sum); }, + norm(v) { var len = Vector.length(v); return [v.x/len, v.y/len]; - }, - make(x, y) { - var vec = Object.create(this, {x:x, y:y}); - }, - + project(a, b) { return cmd(85, a, b); }, diff --git a/scripts/debug.js b/scripts/debug.js index 8fe07dc..7047bdd 100644 --- a/scripts/debug.js +++ b/scripts/debug.js @@ -6,11 +6,41 @@ var Shape = { color ??= Color.blue; Shape.circle(pos,size,color); }, - - arrow(start, end, color, capsize) { + + /* size here is arm length - size of 2 is 4 height total */ + cross(pos, size, color, angle) { + angle ??= 0; color ??= Color.red; - capsize ??= 4; - cmd(81, start, end, color, capsize); + var a = [ + pos.add([0,size]), + pos.add([0,-size]) + ]; + var b = [ + pos.add([size,0]), + pos.add([-size,0]) + ]; + + Shape.line(a,color); + Shape.line(b,color); + }, + + arrow(start, end, color, wingspan, wingangle) { + color ??= Color.red; + wingspan ??= 4; + wingangle ??=10; + + var dir = end.sub(start).normalized(); + var wing1 = [ + Vector.rotate(dir, wingangle).scale(wingspan).add(end), + end + ]; + var wing2 = [ + Vector.rotate(dir,-wingangle).scale(wingspan).add(end), + end + ]; + Shape.line([start,end],color); + Shape.line(wing1,color); + Shape.line(wing2,color); }, poly(points, color) { cmd_points(0,points,color); }, diff --git a/scripts/editor.js b/scripts/editor.js index 7e7bbd4..6f738cc 100644 --- a/scripts/editor.js +++ b/scripts/editor.js @@ -367,8 +367,6 @@ var editor = { color_depths: [], draw() { - Shape.point(world2screen(this.cursor), 2, Color.green); - this.selectlist.forEach(x => { if ('gizmo' in x && typeof x['gizmo'] === 'function' ) x.gizmo(); @@ -392,12 +390,14 @@ var editor = { Shape.line(bb2points(bb).wrapped(1), Color.white); } - Debug.coordinate([0,0]); + }, gui() { /* Clean out killed objects */ this.selectlist = this.selectlist.filter(function(x) { return x.alive; }); + Debug.coordinate(world2screen([0,0])); + Shape.cross(Mouse.pos, 5); GUI.text("WORKING LAYER: " + this.working_layer, [0,520]); GUI.text("MODE: " + this.edit_mode, [0,500]); @@ -405,7 +405,7 @@ var editor = { if (this.comp_info && this.sel_comp) GUI.text(Input.print_pawn_kbm(this.sel_comp,false), [100,700],1); - GUI.text("+", editor.edit_level.screenpos(), 1, Color.blue); + Shape.cross(editor.edit_level.screenpos(),3,Color.blue); var thiso = editor.get_this(); var clvl = thiso; @@ -459,9 +459,11 @@ var editor = { Object.entries(thiso.objects).forEach(function(x) { var p = x[1].namestr(); GUI.text(p, x[1].screenpos().add([0,16]),1,editor.color_depths[depth]); + Shape.circle(x[1].screenpos(),10,Color.blue); + Shape.arrow(x[1].screenpos(), x[1].screenpos().add([0,50]), Color.red, 10); }); - var mg = physics.pos_query(Mouse.worldpos); + var mg = physics.pos_query(Mouse.worldpos,10); if (mg) { var p = mg.path_from(thiso); @@ -1095,10 +1097,8 @@ editor.inputs.mouse = {}; editor.inputs.mouse.move = function(pos, dpos) { if (editor.mousejoy) { - if (editor.z_start) { + if (editor.z_start) editor.camera.zoom -= dpos.y/500; - console.say(editor.camera.zoom); - } else if (editor.joystart) editor.camera.pos = editor.camera.pos.sub(Game.camera.dir_view2world(dpos)); } diff --git a/scripts/physics.js b/scripts/physics.js index 02b67d0..30e7263 100644 --- a/scripts/physics.js +++ b/scripts/physics.js @@ -19,7 +19,10 @@ var physics = { get gravity() { return cmd(72); }, set damping(x) { cmd(73,Math.clamp(x,0,1)); }, get damping() { return cmd(74); }, - pos_query(pos) { return cmd(44, pos); }, + pos_query(pos, give) { + give ??= 25; + return cmd(44, pos, give); + }, /* Returns a list of body ids that a box collides with */ box_query(box) { return cmd(52, box.pos, box.wh); }, diff --git a/source/engine/2dphysics.c b/source/engine/2dphysics.c index a9426aa..6582c84 100644 --- a/source/engine/2dphysics.c +++ b/source/engine/2dphysics.c @@ -282,7 +282,7 @@ void phys2d_applybox(struct phys2d_box *box) { void phys2d_dbgdrawbox(struct phys2d_box *box) { int n = cpPolyShapeGetCount(box->shape.shape); - HMM_Vec2 points[n * 2]; + HMM_Vec2 points[n+1]; struct gameobject *go = shape2go(box->shape.shape); for (int i = 0; i < n; i++) { @@ -290,12 +290,13 @@ void phys2d_dbgdrawbox(struct phys2d_box *box) { p.cp = cpPolyShapeGetVert(box->shape.shape, i); points[i] = go2world(go, p); } + points[n] = points[0]; struct rgba c = shape_color(box->shape.shape); struct rgba cl = c; cl.a = col_alpha; float seglen = cpShapeGetSensor(box->shape.shape) ? sensor_seg : 0; - draw_line(points, n, cl,seglen, 1, 0); + draw_line(points, n, cl,seglen, 0); draw_poly(points, n, c); } /************** POLYGON ************/ @@ -364,14 +365,16 @@ void phys2d_dbgdrawpoly(struct phys2d_poly *poly) { if (arrlen(poly->points) >= 3) { int n = cpPolyShapeGetCount(poly->shape.shape); - HMM_Vec2 points[n]; + HMM_Vec2 points[n+1]; HMM_Mat3 rt = t_go2world(shape2go(poly->shape.shape)); for (int i = 0; i < n; i++) points[i] = mat_t_pos(rt, (HMM_Vec2)cpPolyShapeGetVert(poly->shape.shape, i)); + points[n] = points[0]; + draw_poly(points, n, color); float seglen = cpShapeGetSensor(poly->shape.shape) ? sensor_seg : 0; - draw_line(points, n, line_color, seglen, 1, 0); + draw_line(points, n, line_color, seglen, 0); } } /****************** EDGE 2D**************/ @@ -503,7 +506,7 @@ void phys2d_dbgdrawedge(struct phys2d_edge *edge) { struct rgba color = shape_color(edge->shapes[0]); struct rgba line_color = color; color.a = col_alpha; - draw_edge(drawpoints, arrlen(edge->points), color, edge->thickness * 2, 0,0, line_color, seglen); + draw_edge(drawpoints, arrlen(edge->points), color, edge->thickness * 2, 0, line_color, seglen); draw_points(drawpoints, arrlen(edge->points), 2, kinematic_color); } diff --git a/source/engine/3d/model.c b/source/engine/3d/model.c index 1846980..b30952e 100644 --- a/source/engine/3d/model.c +++ b/source/engine/3d/model.c @@ -170,7 +170,7 @@ sg_buffer normal_floats(float *f, int verts, int comp) { uint32_t packed_norms[verts]; for (int v = 0, i = 0; v < verts; v++, i+= comp) - packed_norms[v] = pack_int10_n2(i); + packed_norms[v] = pack_int10_n2(f+i); return sg_make_buffer(&(sg_buffer_desc){ .data.ptr = packed_norms, diff --git a/source/engine/debug/debugdraw.c b/source/engine/debug/debugdraw.c index 7303e4f..ae5598f 100644 --- a/source/engine/debug/debugdraw.c +++ b/source/engine/debug/debugdraw.c @@ -324,19 +324,12 @@ void debugdraw_init() }); } -void draw_line(HMM_Vec2 *a_points, int n, struct rgba color, float seg_len, int closed, float seg_speed) +void draw_line(HMM_Vec2 *points, int n, struct rgba color, float seg_len, float seg_speed) { if (n < 2) return; - HMM_Vec2 *points = a_points; + seg_speed = 1; - if (closed) { - n++; - points = malloc(sizeof(HMM_Vec2) * n); - memcpy(points, a_points, sizeof(HMM_Vec2)*(n-1)); - - points[n-1] = points[0]; - } - + struct line_vert v[n]; float dist = 0; @@ -374,11 +367,11 @@ void draw_line(HMM_Vec2 *a_points, int n, struct rgba color, float seg_len, int sg_append_buffer(line_bind.vertex_buffers[0], &vr); sg_append_buffer(line_bind.index_buffer, &ir); - - line_c += i_c; - line_v += n; - if (closed) free(points); + YughWarn("Drew %d line segments with %d verts and %d indexes, starting at %d and %d.", n-1, n, i_c, line_v, line_c); + + line_c += i_c+1; + line_v += n; } HMM_Vec2 center_of_vects(HMM_Vec2 *v, int n) @@ -394,11 +387,7 @@ HMM_Vec2 center_of_vects(HMM_Vec2 *v, int n) return c; } -float vecs2m(HMM_Vec2 a, HMM_Vec2 b) -{ - return (b.y-a.y)/(b.x-a.x); -} - +/* Given a series of points p, computes a new series with them expanded on either side by d */ HMM_Vec2 *inflatepoints(HMM_Vec2 *p, float d, int n) { if (d == 0) { @@ -440,10 +429,16 @@ HMM_Vec2 *inflatepoints(HMM_Vec2 *p, float d, int n) return ret; } -void draw_edge(HMM_Vec2 *points, int n, struct rgba color, int thickness, int closed, int flags, struct rgba line_color, float line_seg) +/* Given a strip of points, draws them as segments. So 5 points is 4 segments, and ultimately 8 vertices */ +void draw_edge(HMM_Vec2 *points, int n, struct rgba color, int thickness, int flags, struct rgba line_color, float line_seg) { - if (thickness == 0) - draw_line(points,n,color,0,closed,0); + int closed = 0; + if (thickness <= 1) { + draw_line(points,n,color,0,0); + return; + } + + return; /* todo: should be dashed, and filled. use a texture. */ /* draw polygon outline */ @@ -496,7 +491,7 @@ void draw_edge(HMM_Vec2 *points, int n, struct rgba color, int thickness, int cl /* Now drawing the line outlines */ if (thickness == 1) { - draw_line(points,n,line_color,line_seg, closed, 0); + draw_line(points,n,line_color,line_seg, 0); } else { HMM_Vec2 in_p[n]; HMM_Vec2 out_p[n]; @@ -518,12 +513,12 @@ void draw_edge(HMM_Vec2 *points, int n, struct rgba color, int thickness, int cl for (int i = n-1, v = n; i >= 0; i--,v++) p[v] = out_p[i]; - draw_line(p,n*2,line_color,line_seg,1,0); + draw_line(p,n*2,line_color,line_seg,0); return; } - draw_line(in_p,n,line_color,line_seg,1,0); - draw_line(out_p,n,line_color,line_seg,1,0); + draw_line(in_p,n,line_color,line_seg,0); + draw_line(out_p,n,line_color,line_seg,0); } } @@ -555,13 +550,6 @@ void draw_box(HMM_Vec2 c, HMM_Vec2 wh, struct rgba color) draw_poly(verts, 4, color); } -void draw_arrow(HMM_Vec2 start, HMM_Vec2 end, struct rgba color, int capsize) -{ - HMM_Vec2 points[2] = {start, end}; - draw_line(points, 2, color, 0, 0,0); - draw_cppoint(end, capsize, color); -} - void draw_grid(float width, float span, struct rgba color) { HMM_Vec2 offset = (HMM_Vec2)cam_pos(); diff --git a/source/engine/debug/debugdraw.h b/source/engine/debug/debugdraw.h index 8ff7d7a..d1d82bd 100644 --- a/source/engine/debug/debugdraw.h +++ b/source/engine/debug/debugdraw.h @@ -9,9 +9,8 @@ void debugdraw_init(); void draw_cppoint(HMM_Vec2 point, float r, struct rgba color); void draw_points(HMM_Vec2 *points, int n, float size, struct rgba color); -void draw_line(HMM_Vec2 *points, int n, struct rgba color, float seg_len, int closed, float seg_speed); -void draw_arrow(HMM_Vec2 start, HMM_Vec2 end, struct rgba, int capsize); -void draw_edge(HMM_Vec2 *points, int n, struct rgba color, int thickness, int closed, int flags, struct rgba line_color, float line_seg); +void draw_line(HMM_Vec2 *points, int n, struct rgba color, float seg_len, float seg_speed); +void draw_edge(HMM_Vec2 *points, int n, struct rgba color, int thickness, int flags, struct rgba line_color, float line_seg); /* pixels - how many pixels thick, segsize - dashed line seg len */ void draw_circle(HMM_Vec2 c, float radius, float pixels, struct rgba color, float seg); diff --git a/source/engine/gameobject.c b/source/engine/gameobject.c index 9432fa8..1c86bde 100644 --- a/source/engine/gameobject.c +++ b/source/engine/gameobject.c @@ -42,7 +42,7 @@ HMM_Mat3 t_world2go(gameobject *go) { return HMM_InvGeneralM3(t_go2world(go)); } HMM_Mat4 t3d_go2world(gameobject *go) { return transform3d2mat(go2t3(go)); } HMM_Mat4 t3d_world2go(gameobject *go) { return HMM_InvGeneralM4(t3d_go2world(go)); } -gameobject *pos2gameobject(HMM_Vec2 pos) { +gameobject *pos2gameobject(HMM_Vec2 pos, float give) { cpShape *hit = phys2d_query_pos(pos.cp); if (hit) @@ -53,7 +53,7 @@ gameobject *pos2gameobject(HMM_Vec2 pos) { HMM_Vec2 gpos = go_pos(gameobjects[i]); float dist = HMM_DistV2(gpos,pos); - if (dist <= 25) return gameobjects[i]; + if (dist <= give) return gameobjects[i]; } return NULL; diff --git a/source/engine/gameobject.h b/source/engine/gameobject.h index 6c1b1e0..a28129c 100644 --- a/source/engine/gameobject.h +++ b/source/engine/gameobject.h @@ -79,7 +79,7 @@ gameobject *shape2go(cpShape *shape); void go_shape_apply(cpBody *body, cpShape *shape, gameobject *go); /* Tries a few methods to select a gameobject; if none is selected returns -1 */ -gameobject *pos2gameobject(HMM_Vec2 pos); +gameobject *pos2gameobject(HMM_Vec2 pos, float give); void gameobject_draw_debug(gameobject *go); void gameobject_draw_debugs(); diff --git a/source/engine/jsffi.c b/source/engine/jsffi.c index e4d5658..0c7ce76 100644 --- a/source/engine/jsffi.c +++ b/source/engine/jsffi.c @@ -700,7 +700,7 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) break; case 44: - go = pos2gameobject(js2vec2(argv[1])); + go = pos2gameobject(js2vec2(argv[1]), js2number(argv[2])); ret = go ? JS_DupValue(js,go->ref) : JS_UNDEFINED; break; @@ -855,7 +855,6 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) break; case 81: - draw_arrow(js2vec2(argv[1]), js2vec2(argv[2]), js2color(argv[3]), js2int(argv[4])); break; case 82: @@ -864,7 +863,7 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) case 83: v1 = js2cpvec2arr(argv[1]); - draw_edge(v1, js_arrlen(argv[1]), js2color(argv[2]), js2number(argv[3]), 0, 0, js2color(argv[2]), 10); + draw_edge(v1, js_arrlen(argv[1]), js2color(argv[2]), js2number(argv[3]), 0, js2color(argv[2]), 10); break; case 84: