5 Replies - 395 Views - Last Post: 28 April 2014 - 06:53 PM Rate Topic: -----

#1 alienfrenZy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-April 14

Unable to place a collision box on top of sprites?

Posted 24 April 2014 - 01:21 PM

Hi people,

I was following along to "Let's Make An RPG (C++/SDL2) - Tutorials" on Youtube and got stuck on this video. Let's Make an RPG (C++/SDL2) - Part 35 Continuation on Collision

I'm trying to make collision detection. I want to place a collision box over all my sprites. So far, I can only get the box to the top left corner of the screen. No errors are being detected. I've followed the video perfectly so I believe. I'll show you the code that I think is having a problem.

The problem is definitely something to do with the setX and setY as I don't think they are getting values for x and y.
I'm using Code::Blocks with SDL2. I've tried to give SetX and SetY hard coded values but it did nothing. Everything still compiles. It must be something small that I can't see. If you need to see the whole project please let me know.

Someone please help, I've been at this for hours. Thank you.

This is the CollisionRectangle header file

#pragma once
#include "stdafx.h"



class CollisionRectangle
{
    public:
        CollisionRectangle();
        CollisionRectangle(int x, int y, int w, int h);
        ~CollisionRectangle(void);

        void SetRectangle(int x, int y, int w, int h);

        SDL_Rect GetRectangle(){ return CollisionRect; }

        void setX(int x){ CollisionRect.x = x + OffsetX; }


        void setY(int y){ CollisionRect.y = y + OffsetY; }

    private:

        int OffsetX;
        int OffsetY;
        SDL_Rect CollisionRect;
};


 


This is the CollisionRectangle .cpp file

#include "CollisionRectangle.h"

CollisionRectangle::CollisionRectangle()
{
    //OffsetX = 0;
    //OffsetY = 0;

 //SetRectangle(0,0,0,0);
}

CollisionRectangle::CollisionRectangle(int x, int y, int w, int h)
{

    OffsetX = x;
    OffsetY = y;
    SetRectangle(0,0,w,h);


}

CollisionRectangle::~CollisionRectangle()
{
    //dtor
}

void CollisionRectangle::SetRectangle(int x, int y, int w, int h)
{
    CollisionRect.x = x + OffsetY;
    CollisionRect.y = y + OffsetY;
    CollisionRect.w = w;
    CollisionRect.h = h;
}



#pragma once
#include "stdafx.h"
#include "SDL_Setup.h"
#include "CollisionRectangle.h"


class Sprite
{
    public:
        Sprite(SDL_Renderer* passed_renderer, std::string FilePath, int x, int y, int w, int h, float *CameraX, float *CameraY, CollisionRectangle passed_CollisionRect);
        ~Sprite();

         void Draw();
         void DrawSteady();

    void SetX(float X);
    void SetY(float Y);
    void SetPosition(float X, float Y);

    float getX();
    float getY();

    void SetOrigin(float X, float Y);

    int GetWidth();
    int GetHeight();

    void SetHeight(int H);
    void SetWidth(int W);

    void PlayAnimation(int BeginFrame, int EndFrame, int Row, float Speed);
    void SetUpAnimation(int passed_Amount_X, int passed_Amount_Y);


    private:

        CollisionRectangle CollisionRect;



        SDL_Rect Camera;
       SDL_Rect CollisionSDL_Rect;

        float *CameraX;
        float *CameraY;

        float Origin_X;
        float Origin_Y;
        float X_pos;
        float Y_pos;

        SDL_Texture* image;
        SDL_Texture* CollisionImage;

        SDL_Rect rect;
        SDL_Rect crop;
        SDL_Renderer* renderer;

        int img_width;
        int img_height;
        int CurrentFrame;
        int animationDelay;

        int Amount_Frame_X;
        int Amount_Frame_Y;


};

 


This is the Sprites header file

#pragma once
#include "stdafx.h"
#include "SDL_Setup.h"
#include "CollisionRectangle.h"


class Sprite
{
    public:
        Sprite(SDL_Renderer* passed_renderer, std::string FilePath, int x, int y, int w, int h, float *CameraX, float *CameraY, CollisionRectangle passed_CollisionRect);
        ~Sprite();

         void Draw();
         void DrawSteady();

    void SetX(float X);
    void SetY(float Y);
    void SetPosition(float X, float Y);

    float getX();
    float getY();

    void SetOrigin(float X, float Y);

    int GetWidth();
    int GetHeight();

    void SetHeight(int H);
    void SetWidth(int W);

    void PlayAnimation(int BeginFrame, int EndFrame, int Row, float Speed);
    void SetUpAnimation(int passed_Amount_X, int passed_Amount_Y);


    private:

        CollisionRectangle CollisionRect;



        SDL_Rect Camera;
       SDL_Rect CollisionSDL_Rect;

        float *CameraX;
        float *CameraY;

        float Origin_X;
        float Origin_Y;
        float X_pos;
        float Y_pos;

        SDL_Texture* image;
        SDL_Texture* CollisionImage;

        SDL_Rect rect;
        SDL_Rect crop;
        SDL_Renderer* renderer;

        int img_width;
        int img_height;
        int CurrentFrame;
        int animationDelay;

        int Amount_Frame_X;
        int Amount_Frame_Y;


};

 


This is the Sprites .cpp file

#include "Sprite.h"

Sprite::Sprite(SDL_Renderer* passed_renderer, std::string FilePath, int x, int y, int w, int h, float *passed_CameraX, float *passed_CameraY, CollisionRectangle passed_CollisionRect)
{
    CollisionRect = passed_CollisionRect;
    renderer = passed_renderer;

    CollisionSDL_Rect = CollisionRect.GetRectangle();


    image = NULL;
    image = IMG_LoadTexture(renderer, FilePath.c_str());

    if (image == NULL)
    {
    std::cout << "Couldn't load " << FilePath.c_str() << std::endl;
    }

    CollisionImage = NULL;
    CollisionImage = IMG_LoadTexture(renderer, "Data/DebugImages/DebugBox.png");

    if (CollisionImage == NULL)
    {
    std::cout << "Couldn't load " << "CollisionImage" << std::endl;
    }

    rect.x = x;
    rect.y = y;
    rect.w = w;
    rect.h = h;

    SDL_QueryTexture(image,NULL,NULL, &img_width, &img_height);

    crop.x = 0;
    crop.y = 0;
    crop.w = img_width;
    crop.h = img_height;

    X_pos = x;
    Y_pos = y;

    Origin_X = 0;
    Origin_Y = 0;

    CurrentFrame = 0;

    Amount_Frame_X = 0;
    Amount_Frame_Y = 0;


    CameraX = passed_CameraX;
    CameraY = passed_CameraY;
    Camera.x = rect.x + *CameraX;
    Camera.y = rect.y + *CameraY;
    Camera.w = rect.w;
    Camera.h = rect.h;
}

void Sprite::DrawSteady()
{
    SDL_RenderCopy(renderer,image, &crop, &rect);
}

void Sprite::Draw()
{


    Camera.x = rect.x + *CameraX;
    Camera.y = rect.y + *CameraY;

  CollisionRect.setX(rect.x + *CameraX);
  CollisionRect.setY(rect.y + *CameraY);

    SDL_RenderCopy(renderer,image, &crop, &Camera);



    SDL_RenderCopy(renderer,CollisionImage, NULL, & CollisionSDL_Rect);
}


void Sprite::SetUpAnimation(int passed_Amount_X, int passed_Amount_Y)
{
    Amount_Frame_X = passed_Amount_X;
    Amount_Frame_Y = passed_Amount_Y;
}

void Sprite::PlayAnimation(int BeginFrame, int EndFrame, int Row, float Speed)
{
    if (animationDelay+Speed < SDL_GetTicks())
    {
     if (EndFrame <= CurrentFrame)
         CurrentFrame = BeginFrame;
    else

    CurrentFrame++;

    crop.x = CurrentFrame * (img_width/Amount_Frame_X);
    crop.y = Row * (img_height/Amount_Frame_Y);
    crop.w = img_width/Amount_Frame_X;
    crop.h = img_height/Amount_Frame_Y;
    animationDelay = SDL_GetTicks();
    }
}

Sprite::~Sprite()
{
    SDL_DestroyTexture(image);
}

void Sprite::SetX(float X)
{
    X_pos = X;
    rect.x = int(X_pos - Origin_X);
}

void Sprite::SetY(float Y)
{
    Y_pos = Y;
    rect.y = int(Y_pos - Origin_Y);
}

void Sprite::SetPosition(float X, float Y)
{
     X_pos = X;
     Y_pos = Y;
 rect.x = int(X_pos - Origin_X);
 rect.y = int(Y_pos - Origin_Y);
}

float Sprite::getX()
{
    return X_pos;

}

float Sprite::getY()
{
    return Y_pos;
}

void Sprite::SetOrigin(float X, float Y)
{
    Origin_X = X;
    Origin_Y = Y;
    SetPosition(getX(),getY());

}

void Sprite::SetWidth(int W)
{
    rect.w = W;
}

void Sprite::SetHeight(int H)
{
    rect.h = H;
}

int Sprite::GetWidth()
{
   return rect.w;
}

int Sprite::GetHeight()
{
   return rect.h;
}
 


Is This A Good Question/Topic? 0
  • +

Replies To: Unable to place a collision box on top of sprites?

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1675
  • View blog
  • Posts: 3,171
  • Joined: 30-May 10

Re: Unable to place a collision box on top of sprites?

Posted 24 April 2014 - 10:07 PM

> 051 CameraX = passed_CameraX;
> 052 CameraY = passed_CameraY;
Try making an actual copy of the data, rather than just storing a pointer to something outside the class.
How do you know what you're pointing at is still there?

> 068 Camera.x = rect.x + *CameraX;
> 069 Camera.y = rect.y + *CameraY;
> 070
> 071 CollisionRect.setX(rect.x + *CameraX);
> 072 CollisionRect.setY(rect.y + *CameraY);
Add print statements (to the console), or use a debugger (much better long term idea) to verify that you're getting proper values from these external pointers.
Was This Post Helpful? 1
  • +
  • -

#3 alienfrenZy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-April 14

Re: Unable to place a collision box on top of sprites?

Posted 25 April 2014 - 08:21 AM

View PostSalem_c, on 25 April 2014 - 05:07 AM, said:

> 051 CameraX = passed_CameraX;
> 052 CameraY = passed_CameraY;
Try making an actual copy of the data, rather than just storing a pointer to something outside the class.
How do you know what you're pointing at is still there?

> 068 Camera.x = rect.x + *CameraX;
> 069 Camera.y = rect.y + *CameraY;
> 070
> 071 CollisionRect.setX(rect.x + *CameraX);
> 072 CollisionRect.setY(rect.y + *CameraY);
Add print statements (to the console), or use a debugger (much better long term idea) to verify that you're getting proper values from these external pointers.


Hi Salem_C, thanks for replying. I am so noob to programming. Thank for having the patience to try and help me. How do you make an actual copy of the data? Do I just make a normal variable for example in the sample above
>052  CameraX = passed_CameraX;
      > 052 CameraY = passed_CameraY; 

should I just do something like
float CameraX;
float CameraX2;
float CameraY;
float CameraY2;

CameraX = CameraX2;
CameraY = CameraY2;


/*** Instead of this....... 
 Sprite::Sprite(SDL_Renderer* passed_renderer, std::string FilePath, int x, int y, int w, int h, float *passed_CameraX, float *passed_CameraY, CollisionRectangle passed_CollisionRect)*/


// This
Sprite::Sprite(SDL_Renderer* passed_renderer, std::string FilePath, int x, int y, int w, int h, float CameraX2, float CameraX2, CollisionRectangle passed_CollisionRect)




How do I get the value of CollisionRect.setX() and CollisionRect.set() to print out to screen. I've tried

std::cout << CollisionRect.setX() << std::endl;


Thanks again for the help.
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1343
  • View blog
  • Posts: 4,606
  • Joined: 19-February 09

Re: Unable to place a collision box on top of sprites?

Posted 25 April 2014 - 10:01 AM

Hi, you have pointers in your Sprite class, you could make them variables I think Salem_c is saying :-

44        float *CameraX;
45        float *CameraY;



Pointers

When a class function is named set it usually means to put data into the class. A get function usually retrieves data from a class, although it is sometimes used to get data from a user.
Was This Post Helpful? 1
  • +
  • -

#5 alienfrenZy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-April 14

Re: Unable to place a collision box on top of sprites?

Posted 25 April 2014 - 11:08 AM

View Post#define, on 25 April 2014 - 05:01 PM, said:

Hi, you have pointers in your Sprite class, you could make them variables I think Salem_c is saying :-

44        float *CameraX;
45        float *CameraY;



Pointers

When a class function is named set it usually means to put data into the class. A get function usually retrieves data from a class, although it is sometimes used to get data from a user.


Hi #define, thanks fro trying to help. I tried changing that from *CameraX to CameraX, hundreds of errors pop up. The camera has been working fine all along. I think that the camera pointers are burried too deep at this stage to go back and change. I'm still very new to programming so I might mess it up more if I do try that.

I think I found what the problem is. I was getting the following error:
C:\SDL\First RPG\src\Sprite.cpp|84|error: taking address of temporary [-fpermissive]|

 CollisionRect.setX(rect.x + *CameraX);
  CollisionRect.setY(rect.y + *CameraY);

  //std::cout << rect.x << std::endl;


    SDL_RenderCopy(renderer,image, &crop, &Camera);

    SDL_RenderCopy(renderer,CollisionImage, NULL, &CollisionRect.GetRectangle());



&CollisionRect.GetRectangle() is what is causing the problem. I fixed this by creating a new variable SDL_Rect collisionSDLRect; Then in the Sprite constructor did collisionSDLRect = collisionRect.GetRectangle(); then in Draw() I had: SDL_RenderCopy(renderer, collisionImage, NULL, &collisionSDLRect);
SDL_Rect collisionSDLRect;
collisionSDLRect = CollisionRect.GetRectangle();
SDL_RenderCopy(renderer, CollisionImage, NULL, &CollisionSDLRect); 


I think this is why I can't proper values for CollisionRect. Is it possible that this is after creating a new rectangle, and setX() and setY() values are being given to this new rectangle (CollisionSDLRect) rather than the original one (CollisionRect)?

If so, how do I fix this? Thanks a million to both of you.
Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1343
  • View blog
  • Posts: 4,606
  • Joined: 19-February 09

Re: Unable to place a collision box on top of sprites?

Posted 28 April 2014 - 06:53 PM

Hi, when you add the offsets you are adding the Y offset to both x and y.

void CollisionRectangle::SetRectangle(int x, int y, int w, int h)
{
    CollisionRect.x = x + OffsetY;
    CollisionRect.y = y + OffsetY;
    CollisionRect.w = w;
    CollisionRect.h = h;
}




Perhaps you have found it by now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1