Jack-Ji / jok

A minimal 2d/3d game framework for @ziglang.
MIT License
172 stars 6 forks source link

cp dynamic position not changes on update #13

Closed MartinAbilev closed 3 months ago

MartinAbilev commented 3 months ago

so i created just one dynamic object globaly

 const   dynamic_body: cp.World.ObjectOption.BodyProperty = .{
            .dynamic = .{
                .position = .{
                    .x = 0,
                    .y = 0,
                },
            },
        };

add to world in init

        _= try world.addObject(.{
            .body = dynamic_body,
            .shapes = &.{
                .{
                    .circle = .{
                        .radius = 15,
                        .physics = .{
                                    .weight = .{ .mass = 1 },
                                    .elasticity = 0.5,
                                },
                    },
                },
            },
        });

then on update try to read its position, but on every step value is the same :( where on screen object falls down all work as expected...

pub fn update(ctx: jok.Context) !void {
    // _ = ctx;
        world.update(ctx.deltaSeconds()) 
        const px: i32 = @intFromFloat(dynamic_body.dynamic.position.x);
        const py: i32 = @intFromFloat(dynamic_body.dynamic.position.y);
        print("bug {} x, y: {}, {}\n", .{0, px, py});
}

do i mising something agen ?

Jack-Ji commented 3 months ago

You need to update the physical status within chipmunk, by calling world.update(ctx.deltaSeconds());. I recommend reading chipmunk's official document, its very well written.

Jack-Ji commented 3 months ago

OK, I think your real problem is from reusing dynamic_body, which is only for initialization. In order to get updated position, you should use index returned by world.addObject to access array world.objects, which contains cpBody/cpShape. jok only get you this far, you need to use c api of chipmunk to get further information, such as position/rotation etc.

MartinAbilev commented 3 months ago

thanks for answer. i got world.update(ctx.deltaSeconds()) in my code i just forget type it here.

i looked at chipmunk api. i bet it here kinda z "problem" i got. now

        const b  =   world.objects.items[0].body;

        print("b {}\n", .{b});

gives error: cannot format optional without a specifier (i.e. {?} or {any}) @compileError("cannot format optional without a specifier (i.e. {?} or {any})");

sory if im sound bit anoying . but cn ya give smal example how to read this pos from this object list ?? :) plese please im stuck with my project without it!

MartinAbilev commented 3 months ago

from api: Chipmunk provides getter/setter functions for a number of properties on rigid bodies. cpVect cpBodyGetPosition(const cpBody body) void cpBodySetPosition(cpBody body, cpVect pos) Position of the body. When changing the position you may also want to call cpSpace.....

i try kinda

        const b =   world.objects.items[0].body;
        const bv = cp.cpBodyGetPosition(b);

        print("b {}\n", .{bv});

error: root struct of file 'deps.chipmunk.chipmunk' has no member named 'cpBodyGetPosition' const bv = cp.cpBodyGetPosition(b);

so no much chips api can help me here

Jack-Ji commented 3 months ago

Try cp.c.cpBodyGetPosition. One more thing, world.objects.items[0].body is optional type, you might consider using .? operator to get real pointer.

It seems you are not familiar with basic zig knowledge well enough, I recommend you to read official documentation on ziglang.org more thoroughly. Its required to be able to dig into official document and source code when working with foss project, especially if you choose to use a programming language still being developed like zig.

MartinAbilev commented 3 months ago

yuhuu thanks it works now :) yes i just started with zig so sometimes get stuck.

        const b =   world.objects.items[0].body.?;
        const bv = cp.c.cpBodyGetPosition(b);

        print("b {}\n", .{bv});

b deps.chipmunk.c.struct_cpVect{ .x = 0e0, .y = 1.0546666e3 } b deps.chipmunk.c.struct_cpVect{ .x = 0e0, .y = 1.0735e3 } b deps.chipmunk.c.struct_cpVect{ .x = 0e0, .y = 1.0925e3 } b deps.chipmunk.c.struct_cpVect{ .x = 0e0, .y = 1.1116666e3 } b deps.chipmunk.c.struct_cpVect{ .x = 0e0, .y = 1.131e3 } b deps.chipmunk.c.struct_cpVect{ .x = 0e0, .y = 1.1505e3 }.....