Home>

Question 1. The player is at first as shown in the image, but after a few seconds it disappears from the screen, and the Y-axis value of the above pos coordinate for debugging is buggy. What did this do? Also, since the drawing is not touched, it is not included in the presentation code.
Question 2: Will smart pointers cause any problems?

I think that the most suspicious is the pos of Collision_Update () {}, create a value in the future to see the moved value, and put it in pos at the end. I do n’t know. Although the debug log was displayed, the value is certainly wrong.

※ Additional
If I make a future variable a value type, it works fine. Why does this happen when I reference it? The problem has been solved, but the cause is worrisome because it occurs when the reference type is used.

Debug log Log.txt

]

#include "DxLib.h"
#include<vector>
#include<math.h>
#include<fstream>
#include "string.h"
#include<iostream>
#include "frame.h"
#include<memory>
#define DEBUG 1 // Enable debugging 1
std :: ofstream ofs ("Log.txt");

#define CELL ((int) 64) //
/ * Speed ​​related * /
#define MOVE 5.0f;// Movement speed

/ * Gravity relation * /
#define GRAVITY_FORCE -2 // decreasing number
#define GRAVITY_MAX -10 // Maximum drop speed
float gravity = 0;// falling gravity /

/ * Jump relationship * /
float jump;
class game
{
private:/* for debugging * /
    #if DEBUG
    int t = 0;
    #endif
private:
    enum class key {
        Up,
        Down,
        Left,
        Right,
        Jump, // space key
        Invalid, // Invalid
    };
    class Position {
    public:
        float x;
        float y;
        Position () {
            x = 0.0f;
            y = 0.0f;
        }
        / *
        Position operator + (Position p)
        {
            // this + p;
        // return p;
        }
        * /
    };

    std :: unique_ptr<Position>pos;// coordinates /////////////////////////////////////// ////
    std :: unique_ptr<Position>move;// Moving factor /////////////////////////////////////
    key * State;// Key state

public:
    int map [100] [100] =
    {
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 , 0,0},


        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 , 0,0},


        {1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0 , 0,0},


        {1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0 , 0,0},


        {1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0 , 0,0},{1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0 , 0,0},


        {1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0 , 0,0},


        {1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0 , 0,0},


        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0 , 0,0},


        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 , 0,0},


        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 , 0,0},


        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 , 0,0},


    };

    int player_graph [10];
    int map_graph [40];
    bool isGround;
    int rev;
    bool jump = false;
    int aniClip;// Anime number management
    game ():
        pos (std :: make_unique<Position>()),
        move (std :: make_unique<Position>())

    {

        pos->x = 100.0f;
        pos->y = 0.0f;
        State = new key;
        rev = 0;
        aniClip = 0;
        isGround = false;
        LoadDivGraph ("mario_resource/Mario_64px.png", 7, 7, 1, 64, 64, player_graph);
        LoadDivGraph ("mario_resource/Block_64px.png", 40,10,4,64,64, map_graph);
    }
    / * Function to reverse the sign of Y * /
    float rev_y (float t)
    {
        return t * -1.0f;
    }

    / * Y-fall of constant velocity linear motion return Negative value * /
    float gravity_force () {
        if (gravity>GRAVITY_FORCE) {
            gravity + = -3;
            return gravity;
        } else {
            return gravity = GRAVITY_MAX;
        }
    }
    /*Key input*/
    void key_Update ()
    {
        if (keybord (KEY_INPUT_LEFT)>0) // ←
        {
            DrawFormatString (100, 100, GetColor (255, 255, 255), "Left", true);
            move->x = -1;
            * State = key :: Left;
        } else if (keybord (KEY_INPUT_RIGHT)>0) // →
        {
            move->x = 1;
            * State = key :: Right;
        } else if (keybord (KEY_INPUT_UP)>0) // Up
        {
            * State = key :: Up;
        } else if (keybord (KEY_INPUT_DOWN)>0) // down
        {
            * State = key :: Down;
        } else if (keybord (KEY_INPUT_SPACE)>0) // Jump
        {
            * State = key :: Jump;
        } else // when nothing is pressed{
            * State = key :: Invalid;
        }
    }
    int d;
    / * Move process * /
    void Move ()
    {
        if (* State == key :: Left)
        {
            move->x = move->x * MOVE;
        } else if (* State == key :: Right)
        {
            move->x = move->x * MOVE;
        }
        else if (* State == key :: Invalid) // when not moving left and right
        {
            move->x = 0;
        }

        move->y + = rev_y (gravity_force ());
        // d = move->x;
    }

    / * Cell search collision: true * /
    bool Find_Cell (Position * p)
    {
        int px = (int) p->x/CELL;
        int py = (int) p->y/CELL;
        if (map [py] [px] == 1)
        {
            return true;
        } else {
        return false;
        }
    }
    int dd = 0;

    / * ------------------------------------------------ --- Hit judgment --------------------------------------------- ------------------- * /
    void Collision_Update ()
    {
        // Position * future = new Position ();
        std :: unique_ptr<Position>future (std :: make_unique<Position>());/////////////////////////
        future->x = pos->x;
        future->y = pos->y;

        / *
        p->x = future->x + move->x;
        p->y = future->y + gravity_force ();
        p2->x = future->x + move->x;
        p2->y = future->y + gravity_force ();
        * /
        / * X correction * /
        / * ----------------- Right --------------- * /
        if (* State == key :: Right)
        {
            Position p;
            p.x = future->x + move->x + CELL;
            p.y = future->y;

            if (Find_Cell (&p) == true)
            {
                dd = 1;
                int px = ((int) p.x/CELL) * CELL;
                px-= CELL;// minus
                future->x = px;
            }
            else {
                Position p2;
                p2.x = future->x + move->x + CELL;
                p2.y = future->y + CELL;
                if (Find_Cell (&p2) == true&&isGround == false)
                {
                    int px = (p2.x/CELL) * CELL;
                    px + = rev_y (CELL);// minus
                    future->x = px;
                }
                else {// X when not hit
                    future->x + = move->x;
                }
            }
        }
        / * ----------------- Left ----------------- * /if (* State == key :: Left)
        {
            Position p;
            p.x = future->x + move->x;
            p.y = future->y;

            if (Find_Cell (&p) == true)
            {
                int px = ((int) p.x/CELL) * CELL;
                px + = CELL;
                future->x = px;
            }
            else {
                Position p2;
                p2.x = future->x + move->x;
                p2.y = future->y + CELL;
                if (Find_Cell (&p2) == true&&isGround == false)
                {
                    int px = ((int) p2.x/CELL) * CELL;
                    px + = CELL;
                    future->x = px;
                }
                else {// X when not hit
                }
                future->x + = move->x;
            }
        }

        /*--------------gravity-----*/
// Position * p = new Position ();
        Position p;
        p.x = future->x;
        p.y = future->y + move->y;
        p.y + = CELL;
        if (Find_Cell (&p) == true)
        {
            int py = ((int) p.y/CELL) * CELL;
            py + = rev_y (CELL);
            future->y = py;
            isGround = true;
        }
        else
        {
            p.x + = CELL;
            if (Find_Cell (&p) == true)
            {
                int py = ((int) p.y/CELL) * CELL;
                py + = rev_y (CELL);
                future->y = py;
                isGround = true;
            }
            else {// Y when not hit
                if (isGround == false) {
                    future->y + = move->y;
                }
                // isGround = false;
            }
        }
        ofs<<"future->:"<<future->x<<","<<future->y<<std :: endl;
        * pos = * future;

    }
    / * ------------------------------------------------ -------------------------------------------------- ----------------------- * /

    / * ----------------------------- Calculation ------------------ ---------- * /
    void const Update ()
    {
        key_Update ();
        Move ();
        Collision_Update ();
        DrawFormatString (0,0, GetColor (255,255,255), "Pos:% .2f,% .2f", pos->x, pos->y, true);

            DrawFormatString (200,0, GetColor (255,255,255), "frame:% d", Fps :: now (), true);
            DrawFormatString (200, 100, GetColor (255, 255, 255), "move:% .2f", move->x, true);
            // DrawFormatString (100, 100, GetColor (255, 255, 255), "Right", true);
            DrawFormatString (100, 100, GetColor (255, 255, 255), "% d", dd, true);


    }
    / * ------------------------------------------------ ------------- */
  • Answer # 1

    /* Y-fall of constant speed linear motion return Negative value * /
    float gravity_force () {
        if (gravity>GRAVITY_FORCE) {
            gravity + = -3;
            return gravity;
        } else {
            return gravity = GRAVITY_MAX;
        }
    }

    This is just my intuition, but isn't the gravity value blowing away at a certain time?
    I feel that the definition value of GRAVITY_MAX is commented out and the value of gravity cannot be controlled.
    Originally the value of gravity should be fixed. Also check the value of

    m

    move->y.
    Although this value seems to be updated more and more in move (),
    Does the absolute value stay within the expected value without ever increasing?