Skip to content

Instantly share code, notes, and snippets.

@celsowm
Created July 26, 2025 03:47
Show Gist options
  • Save celsowm/767f518db582e29ef9dd43b906bede39 to your computer and use it in GitHub Desktop.
Save celsowm/767f518db582e29ef9dd43b906bede39 to your computer and use it in GitHub Desktop.
red cube and jump jo engine
/*
* Cubo 3‑D com pulo correto – JoEngine
* Controles: D‑Pad move | Botão A pula
*/
#include <jo/jo.h>
#include <stdbool.h>
/* ---------- parâmetros ---------- */
#define CUBE_SIZE 32
#define MOVE_SPEED 3
#define GRAVITY 1 /* puxa para baixo ( +Y ) */
#define JUMP_START -14 /* impulso para cima ( –Y ) */
/* câmera */
#define CAM_DIST -520
#define CAM_HEIGHT 40
#define FOV_DEGREE 35
/* geometria */
static jo_camera cam;
static jo_vertice cube_vertices[] = JO_3D_CUBE_VERTICES(CUBE_SIZE);
static jo_3d_quad cube_quads[6];
static jo_vertice floor_vertices[] = JO_3D_PLANE_VERTICES(256);
static jo_3d_quad floor_quad;
/* estado */
static int pos_x = 0, pos_y = 0, pos_z = 0;
static int vel_y = 0;
static bool jumping = false;
/* ---------- criação ---------- */
static void create_cube(void)
{
jo_3d_create_cube(cube_quads, cube_vertices);
for (int i = 0; i < 6; ++i)
jo_3d_set_color(&cube_quads[i], JO_COLOR_Red);
}
static void create_floor(void)
{
jo_3d_create_plane(&floor_quad, floor_vertices);
jo_3d_set_color(&floor_quad, JO_COLOR_Gray);
}
/* ---------- entrada + física ---------- */
static void update_physics(void)
{
/* mover */
if (jo_is_pad1_key_pressed(JO_KEY_LEFT)) pos_x -= MOVE_SPEED;
if (jo_is_pad1_key_pressed(JO_KEY_RIGHT)) pos_x += MOVE_SPEED;
if (jo_is_pad1_key_pressed(JO_KEY_UP)) pos_z += MOVE_SPEED;
if (jo_is_pad1_key_pressed(JO_KEY_DOWN)) pos_z -= MOVE_SPEED;
/* pular */
if (!jumping && jo_is_pad1_key_pressed(JO_KEY_A))
{
jumping = true;
vel_y = JUMP_START; /* valor negativo sobe */
}
if (jumping)
{
pos_y += vel_y;
vel_y += GRAVITY; /* acelera para baixo */
if (pos_y >= 0) /* tocou o “chão” */
{
pos_y = 0;
vel_y = 0;
jumping = false;
}
}
}
/* ---------- desenho ---------- */
static void draw_floor(void)
{
jo_3d_push_matrix();
jo_3d_rotate_matrix_x(-90); /* deita o plano */
jo_3d_draw(&floor_quad);
jo_3d_pop_matrix();
}
static void draw_cube(void)
{
jo_3d_push_matrix();
jo_3d_translate_matrix(pos_x, pos_y, pos_z);
jo_3d_draw_array(cube_quads, 6);
jo_3d_pop_matrix();
}
/* ---------- loop ---------- */
static void game_loop(void)
{
update_physics();
jo_3d_camera_look_at(&cam);
draw_floor();
draw_cube();
jo_printf(2, 1, "X:%d Y:%d Z:%d ", pos_x, pos_y, pos_z);
jo_printf(2, 2, "Poly: %d ", jo_3d_get_polygon_count());
}
/* ---------- entrada do programa ---------- */
void jo_main(void)
{
jo_core_init(JO_COLOR_Black);
jo_3d_perspective_angle(FOV_DEGREE);
jo_3d_camera_init(&cam);
jo_3d_camera_set_viewpoint(&cam, 0, CAM_HEIGHT, CAM_DIST);
jo_3d_camera_set_target(&cam, 0, 0, 0);
create_cube();
create_floor();
jo_core_add_callback(game_loop);
jo_core_run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment