SGDK: How to make the camera follow the player?

Hi !
I've created a small code of a box going ever to the right and a jump button
But when I tried to move the camera, it did not work
What I did wrong here?

C-like:
#include <jo/jo.h>

// Structure to store the cube's position and velocity
typedef struct
{
    jo_pos3Df position;
    float velocity_y;
    float velocity_x; // Horizontal velocity
    bool is_jumping;
} Cube;

// Cube instance
Cube cube;

// Position and rotation structures
jo_pos3Df pos;
jo_rot3Df rot;
jo_palette image_pal;

// Camera structure
jo_camera cam;

// Definition of the cube's vertices and quads
jo_vertice cube_vertices[] = JO_3D_CUBE_VERTICES(32);
jo_3d_quad cube_quads[6];

// Constants
const float GRAVITY = 1.0f;        // Gravity force
const float JUMP_VELOCITY = 15.0f; // Initial jump velocity
const float GROUND_Y = 0.0f;       // Ground position
const float MOVE_SPEED = 2.0f;     // Horizontal movement speed

// Function to create the cube
void create_cube(void)
{
    int i;
    jo_3d_create_cube(cube_quads, cube_vertices);
    for (i = 0; i < 6; ++i)
        jo_3d_set_texture(&cube_quads[i], 0);
}

// Function to initialize the cube
void init_cube(void)
{
    cube.position.x = 0.0f;
    cube.position.y = GROUND_Y;
    cube.position.z = 0.0f;
    cube.velocity_y = 0.0f;
    cube.velocity_x = MOVE_SPEED; // Initialize horizontal velocity
    cube.is_jumping = false;
}

// Function to update the cube's position
void update_cube_position(void)
{
    // Continuous horizontal movement
    cube.position.x += cube.velocity_x;

    if (cube.is_jumping)
    {
        // Update Y position with the current velocity
        cube.position.y -= cube.velocity_y;
        // Apply gravity
        cube.velocity_y -= GRAVITY;

        // Check if the cube has returned to the ground
        if (cube.position.y >= GROUND_Y)
        {
            cube.position.y = GROUND_Y;
            cube.is_jumping = false;
            cube.velocity_y = 0.0f;
        }
    }
    else
    {
        // Detect button C to initiate a jump
        if (jo_is_pad1_key_down(JO_KEY_C))
        {
            cube.is_jumping = true;
            cube.velocity_y = JUMP_VELOCITY;
        }
    }
}

// Function to draw the cube
void draw_cube(void)
{
    jo_3d_push_matrix();
    {
        jo_3d_translate_matrixf(cube.position.x, cube.position.y, cube.position.z);
        jo_3d_draw_array(cube_quads, 6);
    }
    jo_3d_pop_matrix();
}

// Function to draw the 3D planes (ground)
void draw_3d_planes(void)
{
    // FLOOR
    jo_3d_push_matrix();
    {
        jo_3d_rotate_matrix_rad(rot.rx, rot.ry, rot.rz);
        jo_3d_translate_matrixf(pos.x, pos.y, pos.z);
        jo_background_3d_plane_a_draw(true);
    }
    jo_3d_pop_matrix();
}

// Function to initialize the 3D planes
void init_3d_planes(void)
{
    jo_img_8bits img;

    jo_core_tv_off();
    jo_enable_background_3d_plane(JO_COLOR_Black);

    // FLOOR
    img.data = JO_NULL;
    jo_tga_8bits_loader(&img, JO_ROOT_DIR, "FLOOR.TGA", 0);
    jo_background_3d_plane_a_img(&img, image_pal.id, true, true);
    jo_free_img(&img);

    jo_core_tv_on();
}

void update_camera(void)
{
    // Set the camera position to follow the cube
    int camera_x = (int)cube.position.x;
    int camera_y = (int)(cube.position.y + 100);  // Adjust for a higher view
    int camera_z = (int)(cube.position.z - 200);  // Position the camera behind the cube

    // Set the camera's target (where it is looking)
    int target_x = (int)cube.position.x;
    int target_y = (int)cube.position.y;
    int target_z = (int)cube.position.z;

    // Update the camera's position
    jo_3d_camera_set_viewpoint(&cam, camera_x, camera_y, camera_z);

    // Update the camera's target
    jo_3d_camera_set_target(&cam, target_x, target_y, target_z);
}

// Main drawing function
void my_draw(void)
{
    jo_printf(12, 1, "*Cube Jump Demo*");
    /* Update the camera */
    
    jo_3d_camera_look_at(&cam);
    update_camera();

    draw_cube();
    jo_printf(0, 28, "Polygon count: %d  ", jo_3d_get_polygon_count());
}

// Palette handling function
jo_palette *my_tga_palette_handling(void)
{
    // Create a new palette for each image. Not ideal but works for a demo.
    jo_create_palette(&image_pal);
    return (&image_pal);
}

// Main function
void jo_main(void)
{
    jo_core_init(JO_COLOR_Black);
    jo_set_tga_palette_handling(my_tga_palette_handling);
    init_3d_planes();

    // Set the initial camera position
    pos.x = 800.0f;
    pos.y = 800.0f;
    pos.z = -35.0f;

    // Set the initial rotation
    rot.rx = JO_DEG_TO_RAD(90.0f);
    rot.ry = JO_DEG_TO_RAD(0.0f);
    rot.rz = JO_DEG_TO_RAD(45.0f);

    jo_3d_camera_init(&cam);
    jo_3d_camera_set_viewpoint(&cam, 0, 0, -250);

    // Add the cube's sprite
    jo_sprite_add_tga(JO_ROOT_DIR, "BOX.TGA", JO_COLOR_Transparent);

    // Create and initialize the cube
    create_cube();
    init_cube();

    // Add the callbacks
    jo_core_add_callback(draw_3d_planes);
    jo_core_add_callback(my_draw);
    jo_core_add_callback(update_cube_position);
    jo_core_run();
}
 
Back
Top