line inflation

This commit is contained in:
John Alanbrook 2023-02-15 23:54:05 +00:00
parent 570169ff15
commit e3100e0221
10 changed files with 79 additions and 40 deletions

View file

@ -177,7 +177,9 @@ void init_phys2dshape(struct phys2d_shape *shape, int go, void *data)
void phys2d_shape_del(struct phys2d_shape *shape)
{
if (!shape->shape) return;
cpSpaceRemoveShape(space, shape->shape);
cpShapeFree(shape->shape);
}
/***************** CIRCLE2D *****************/
@ -249,15 +251,14 @@ struct phys2d_box *Make2DBox(int go)
new->r = 0.f;
new->offset[0] = 0.f;
new->offset[1] = 0.f;
new->shape.shape = cpSpaceAddShape(space, cpBoxShapeNew(id2go(go)->body, new->w, new->h, new->r));
new->shape.debugdraw = phys2d_dbgdrawbox;
init_phys2dshape(&new->shape, go, new);
new->shape.go = go;
phys2d_applybox(new);
new->shape.debugdraw = phys2d_dbgdrawbox;
return new;
}
void phys2d_boxdel(struct phys2d_box *box)
{
phys2d_shape_del(&box->shape);
@ -274,19 +275,24 @@ void box_gui(struct phys2d_box *box)
void phys2d_applybox(struct phys2d_box *box)
{
phys2d_boxdel(box);
struct gameobject *go = id2go(box->shape.go);
float s = id2go(box->shape.go)->scale;
cpTransform T = { 0 };
T.a = s * cos(box->rotation);
T.b = -sin(box->rotation);
T.c = sin(box->rotation);
T.b = s * -sin(box->rotation);
T.c = s * sin(box->rotation);
T.d = s * cos(box->rotation);
T.tx = box->offset[0] * s;
T.ty = box->offset[1] * s;
T.tx = box->offset[0] * s * go->flipx;
T.ty = box->offset[1] * s * go->flipy;
float hh = box->h / 2.f;
float hw = box->w / 2.f;
cpVect verts[4] = { { -hw, -hh }, { hw, -hh }, { hw, hh }, { -hw, hh } };
cpPolyShapeSetVerts(box->shape.shape, 4, verts, T);
cpPolyShapeSetRadius(box->shape.shape, box->r);
box->shape.shape = cpSpaceAddShape(space, cpPolyShapeNew(go->body, 4, verts, T, box->r));
init_phys2dshape(&box->shape, box->shape.go, box);
// cpPolyShapeSetVerts(box->shape.shape, 4, verts, T);
// cpPolyShapeSetRadius(box->shape.shape, box->r);
}
void phys2d_dbgdrawbox(struct phys2d_box *box)
{
@ -512,7 +518,7 @@ void phys2d_dbgdrawedge(struct phys2d_edge *edge)
drawpoints[i].y = p.y + d*sin(a);
}
draw_edge(drawpoints, arrlen(edge->points), trigger_color);
draw_edge(drawpoints, arrlen(edge->points), trigger_color, edge->thickness*2);
draw_points(drawpoints, arrlen(edge->points), 2, kinematic_color);
}

View file

@ -64,22 +64,51 @@ void draw_line(int x1, int y1, int x2, int y2, float *color)
draw_poly(verts, 2, color);
}
void draw_edge(cpVect *points, int n, float *color)
cpVect center_of_vects(cpVect *v, int n)
{
cpVect c;
for (int i = 0; i < n; i++) {
c.x += v[i].x;
c.y += v[i].y;
}
c.x /= n;
c.y /= n;
return c;
}
float vecs2m(cpVect a, cpVect b)
{
return (b.y-a.y)/(b.x-a.x);
}
cpVect inflateline(cpVect a, cpVect b, float d)
{
cpVect c;
float m = vecs2m(a, b);
c.x = d/sqrt(1/(pow(m,2)+1));
c.y = d/sqrt(1+pow(m,2));
return c;
}
void draw_edge(cpVect *points, int n, float *color, int thickness)
{
static_assert(sizeof(cpVect) == 2*sizeof(float));
shader_use(rectShader);
shader_setvec3(rectShader, "linecolor", color);
glLineWidth(20);
glBindBuffer(GL_ARRAY_BUFFER, rectVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * n * 2, points, GL_DYNAMIC_DRAW);
glBindVertexArray(rectVAO);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
if (thickness <= 1) {
glBindBuffer(GL_ARRAY_BUFFER, rectVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * n * 2, points, GL_DYNAMIC_DRAW);
glBindVertexArray(rectVAO);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
shader_setfloat(rectShader, "alpha", 1.f);
glDrawArrays(GL_LINE_STRIP, 0, n);
glLineWidth(1);
shader_setfloat(rectShader, "alpha", 1.f);
glDrawArrays(GL_LINE_STRIP, 0, n);
} else {
}
}
void draw_circle(int x, int y, float radius, int pixels, float *color, int fill)

View file

@ -5,7 +5,7 @@ struct cpVect;
void debugdraw_init();
void draw_line(int x1, int y1, int x2, int y2, float *color);
void draw_edge(struct cpVect *points, int n, float *color);
void draw_edge(struct cpVect *points, int n, float *color, int thickness);
void draw_points(struct cpVect *points, int n, float size, float *color);
void draw_circle(int x, int y, float radius, int pixels, float *color, int fill);
void draw_grid(int width, int span);

View file

@ -1027,4 +1027,4 @@ void sprite_gui(struct sprite *sprite) {
}
#endif
#endif

View file

@ -234,6 +234,10 @@ duk_ret_t duk_nuke(duk_context *duk)
nuke_edit_str(textbox);
duk_push_string(duk, textbox);
return 1;
case 8:
nuke_img(duk_to_string(duk, 1));
break;
}
return 0;
@ -293,7 +297,7 @@ duk_ret_t duk_spline_cmd(duk_context *duk)
duk_put_prop_index(duk, arridx, i);
}
ts_bspline_free(&spline);
ts_bspline_free(&spline);
return 1;
}
@ -402,7 +406,7 @@ static int duk2path(const char *path, const struct stat *sb, int typeflag)
if (typeflag == FTW_F) {
char *ext = strrchr(path, '.');
if (ext && !strcmp(ext, dukext)) {
duk_push_string(duk, path);
duk_push_string(duk, &path[2]);
duk_put_prop_index(duk, dukarr, dukidx++);
}
}
@ -916,7 +920,6 @@ duk_ret_t duk_make_sprite(duk_context *duk) {
int go = duk_to_int(duk, 0);
const char *path = duk_to_string(duk, 1);
cpVect pos = duk2vec2(duk, 2);
int sprite = make_sprite(go);
struct sprite *sp = id2sprite(sprite);
sprite_loadtex(sp, path);

View file

@ -84,16 +84,7 @@ void go_shape_apply(cpBody *body, cpShape *shape, struct gameobject *go)
{
cpShapeSetFriction(shape, go->f);
cpShapeSetElasticity(shape, go->e);
cpTransform T = {0};
T.a = go->flipx;
T.d = go->flipy;
cpShapeUpdate(shape, T);
if (go->flipx == -1) YughInfo("Flipped one");
// cpShapeSetFilter(shape, go->filter);
// YughLog("Set filter; %d", go->filter.mask);
}
void gameobject_apply(struct gameobject *go)

View file

@ -140,9 +140,12 @@ void call_input_signal(char *signal) {
if (pawns[i] == NULL) arrdel(pawns, i);
}
for (int i = 0; i < arrlen(pawns); i++) {
if (!pawns[i]) continue;
script_eval_w_env(signal, pawns[i]);
int len = arrlen(pawns);
void *framepawns[len];
memcpy(framepawns, pawns, len*sizeof(*pawns));
for (int i = 0; i < len; i++) {
script_eval_w_env(signal, framepawns[i]);
}
}

View file

@ -16,6 +16,7 @@
#include <stdarg.h>
#include "window.h"
#include "texture.h"
#define MAX_VERTEX_BUFFER 512 * 1024
#define MAX_ELEMENT_BUFFER 128 * 1024
@ -89,6 +90,12 @@ int nuke_btn(const char *lbl) {
return nk_button_label(ctx, lbl);
}
void nuke_img(char *path) {
struct Texture *t = texture_pullfromfile(path);
nk_layout_row_static(ctx, t->height, t->width, 1);
nk_image(ctx, nk_image_id(t->id));
}
void nuke_property_int(const char *lbl, int min, int *val, int max, int step) {
nk_property_int(ctx, lbl, min, val, max, step, step);
}
@ -129,4 +136,4 @@ void nuke_labelf(const char *fmt, ...) {
vsnprintf(buf, 512, fmt, args);
nuke_label(buf);
va_end(args);
}
}

View file

@ -26,6 +26,7 @@ void nuke_nel(int cols);
void nuke_label(const char *s);
void nuke_prop_float(const char *label, float min, float *val, float max, float step, float dragstep);
void nuke_edit_str(char *str);
void nuke_img(char *path);
int nuke_push_tree_id(const char *name, int id);
void nuke_tree_pop();

View file

@ -33,7 +33,6 @@ struct vec *c_vec = NULL;
char pathbuf[MAXPATH];
void resources_init()
{
prefabs = vec_make(MAXNAME, 25);