Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Canvas

Pages: 1 [2] 3 4 ... 8
16
General / Which side of my bounding box hit
« on: October 20, 2014, 09:45:56 pm »
Hello there

I'm having quite some trouble with my collision detection,
First off here is my collision detection method

CollisionSide MathsService::RectToRectCollisionName(int rect1_x, int rect1_y, int rect1_w, int rect1_h, int rect2_x, int rect2_y, int rect2_w, int rect2_h)
{
        bool collision = false;
        // top-left corner
        if (PointInRect(rect1_x, rect1_y, rect2_x, rect2_y, rect2_w, rect2_h)){ collision = true; }
        // top-right corner
        if (PointInRect(rect1_x + rect1_w - 1, rect1_y, rect2_x, rect2_y, rect2_w, rect2_h)){ collision = true; }
        // bottom-right corner
        if (PointInRect(rect1_x + rect1_w - 1, rect1_y + rect1_h - 1, rect2_x, rect2_y, rect2_w, rect2_h)){ collision = true; }
        // bottom-left corner
        if (PointInRect(rect1_x, rect1_y + rect1_h - 1, rect2_x, rect2_y, rect2_w, rect2_h)){ collision = true; }
        // Check to see if rectangle 2 is hit any part of rectanlge 1
        // top-left corner
        if (PointInRect(rect2_x, rect2_y, rect1_x, rect1_y, rect1_w, rect1_h)){ collision = true; }
        // top-right corner
        if (PointInRect(rect2_x + rect2_w - 1, rect2_y, rect1_x, rect1_y, rect1_w, rect1_h)){ collision = true; }
        // bottom-right corner
        if (PointInRect(rect2_x + rect2_w - 1, rect2_y + rect2_h - 1, rect1_x, rect1_y, rect1_w, rect1_h)){ collision = true; }
        // bottom-left corner
        if (PointInRect(rect2_x, rect2_y + rect2_h - 1, rect1_x, rect1_y, rect1_w, rect1_h)){ collision = true; }
        // If there is no collision
        if (collision == true)
        {
                int rect1xcentre = rect1_x + (rect1_w / 2);
                int rect2xcentre = rect2_x + (rect2_w / 2);

                int rect1ycentre = rect1_y + (rect1_h / 2);
                int rect2ycentre = rect2_y + (rect2_h / 2);

                int     xDifference = rect1xcentre - rect2xcentre;
                int yDifference = rect1ycentre - rect2ycentre;

                int xValue = xDifference < 0 ? xDifference * -1 : xDifference;
                int yValue = yDifference < 0 ? yDifference * -1 : yDifference;

                if (xValue > yValue)
                {
                        CollisionSide result;
                        xDifference < 0 ? result = CollisionSide::Left : result = CollisionSide::Right;
                        return result;
                }

                if (yValue > xValue)
                {
                        CollisionSide result;
                        yDifference < 0 ? result = CollisionSide::Top : result = CollisionSide::Bottom;
                        return result;
                }

                return CollisionSide::None;
        }
        else
        {
                return CollisionSide::None;
        }
}
 

This method checks on two rectangles, if they are colliding a side will be return't, however I am getting some odd results.

I have a player which is a 64 by 64 image which can be moving downwards at a total of 16 pixels per tick. Now usually when it hits a block on the floor it will return my enum "Bottom" and the ball will bounce correctly, however now and then the ball get's stuck inside of the block and will go left, right or down. How can I stop this from happening? Do I need to check for a collision 1 tick in front and pre-empt it?

Basically my code to check if the player hits a block is like so

void LevelState::CheckPlayerCollisions()
{
        bool collision = false;
        for (int i = 0; i < DisplayObjects.size(); i++)
        {
                if (_mathsService.RectToRectCollision(
                        DisplayObjects.at(i).XPos,
                        DisplayObjects.at(i).YPos,
                        DisplayObjects.at(i).Sprite.getTextureRect().width,
                        DisplayObjects.at(i).Sprite.getTextureRect().height,
                        Player.XPos - (Player.Sprite.getTextureRect().width / 2),
                        Player.YPos - (Player.Sprite.getTextureRect().height / 2),
                        Player.Sprite.getTextureRect().width,
                        Player.Sprite.getTextureRect().height
                        ))
                {
                        //Work out the force to bound the ball
                        CollisionSide result = _mathsService.RectToRectCollisionName(
                                DisplayObjects.at(i).XPos,
                                DisplayObjects.at(i).YPos,
                                DisplayObjects.at(i).Sprite.getTextureRect().width,
                                DisplayObjects.at(i).Sprite.getTextureRect().height,
                                Player.XPos - (Player.Sprite.getTextureRect().width / 2),
                                Player.YPos - (Player.Sprite.getTextureRect().height / 2),
                                Player.Sprite.getTextureRect().width,
                                Player.Sprite.getTextureRect().height
                                );


                        int force = ceil(Player.YPos / 42);
                        if (force > 16)
                        {
                                force = 16;
                        }
                        std::cout << "Force feed back : " << force << std::endl;
                        Player.HitObjectUpdate(force, result, DisplayObjects.at(i).YPos);
                        collision = true;
                }

                if (collision)
                {
                        break;
                }
        }
}
 

The method above is being working on of course.

If anyone can give me a hand that would be awesome, I can profile screenshots and pictures if needed.

17
General / Re: Vector of custom class, how to populate?
« on: October 09, 2014, 11:40:33 pm »
I started to convert stuff to a list but I had a different Idea, you stated "It's been a while since I used containers of Text/Font so it's not obvious to me exactly what the problem is," so I thought why don't I stop passing in my sf::Font to each TextObject and just pass a font into the DrawTextObjects method, so now I pass in a sf::Font reference to my DrawTextObjects and now it seems to work using this code which is what I wanted :)

TextObjects.push_back(TextObject());
        TextObjects.push_back(TextObject());
        TextObjects.at(0).Setup(200, 350, "Score : " + std::to_string(Score), 24);
        TextObjects.at(0).CentreText();
        TextObjects.at(1).Setup(200, 100, "Game Over", 48);
        TextObjects.at(1).SetRotationAnimation(-12, 12, 0.1);
       
        if (highScore == true)
        {
                TextObjects.push_back(TextObject());
                TextObjects.at(2).Setup(200, 300, "New high score!", 24);
                TextObjects.at(2).CentreText();
        }

Cheers for the help Ixrec

18
General / Re: Vector of custom class, how to populate?
« on: October 09, 2014, 11:11:58 pm »
I will give that a go tomorrow and get back in touch with you Ixrec, I will try to provide cleaner code but an easier way "maybe" is you can download my game here http://www.canvascode.co.uk/Files/SpaceDestroyer.rar. All of my code is inside of that :)

Update

If I was to do a list, how would I set up my TextObjects?

TextObject textObject;
        TextObjects.push_back(textObject);
        TextObjects.push_back(textObject);
        TextObjects.at(0).Setup(200, 100, "Space Destroyer", 32, _fontLoader.Fonts[Main_Font]);
        TextObjects.at(0).SetRotationAnimation(-12, 12, 0.1);
        TextObjects.at(1).Setup(200, 350, "High score : " + std::to_string(score) , 32, _fontLoader.Fonts[Main_Font]);
        TextObjects.at(1).CentreText();

I can't use .at anymore nore


Update

Just quickly Ixrec how would you draw the font? Pass the sf::Font in when the text is to be drawn?

19
General / Re: Vector of custom class, how to populate?
« on: October 09, 2014, 10:00:42 pm »
Ixrec I do apologize about not showing my TextObject code I will show it right here

#include "TextObject.h"

TextObject::TextObject()
{

}

void TextObject::Setup(float xSet, float ySet, std::string textSet, int fontSize, sf::Font& fontSet)
{
        XPos = xSet;
        YPos = ySet;
        TextString = textSet;
        FontSize = fontSize;
        Font = fontSet;
        Animated = false;
        StartRotationPosition = 0;
        EndRotationPosition = 0;
        Rotation = 0;
        RotateSpeed = 0;

        Text.setColor(sf::Color(255, 255, 255, 255));
        Text.setCharacterSize(FontSize);
        Text.setFont(Font);
        Text.setPosition(XPos, YPos);
        Text.setString(TextString);
}

TextObject::~TextObject()
{

}

void TextObject::CentreText()
{
        Text.setOrigin(Text.getLocalBounds().width / 2, Text.getLocalBounds().height / 2);
}

void TextObject::SetRotationAnimation(float startRotation, float endRotation, float speed)
{
        Animated = true;
        StartRotationPosition = startRotation;
        EndRotationPosition = endRotation;

        Rotation = 0;
        RotateSpeed = speed;
}

void TextObject::Draw(sf::RenderWindow& window)
{
        if (Animated == false)
        {
                window.draw(Text);     
        }
        else
        {
                DrawAnimation(window);
        }
       
}

void TextObject::DrawAnimation(sf::RenderWindow& window)
{
        Rotation += RotateSpeed;

        Text.setOrigin(Text.getLocalBounds().width / 2, Text.getLocalBounds().height / 2);
        Text.setRotation(Rotation);

        window.draw(Text);

        if (Rotation >= EndRotationPosition)
        {
                RotateSpeed = -RotateSpeed;
        }

        if (Rotation <= StartRotationPosition)
        {
                RotateSpeed = -RotateSpeed;
        }
}
 

Draw code goes something like this (because there is alot of calling :)

void Game::Draw()
{
        renderService.Window.clear();

        if (gameState == GameStates::MainMein)
        {
                mainMenuState.Draw(renderService.Window);
        }

        renderService.Window.display();
}

//mainMenuState.Draw method below
void State::Draw(sf::RenderWindow& window)
{
        DrawDisplayObjects(window);
        DrawButtonObjects(window);
        DrawTextObjects(window);
}

void State::DrawTextObjects(sf::RenderWindow& window)
{
        for (int i = 0; i < TextObjects.size(); i++)
        {
                TextObjects.at(i).Draw(window);
        }
}

 

Let me know if you need anymore of my code :) I have no problem in showing it all, but I do have a lot of classes

Update

Go in with the debugger it gets to this

Font.cpp
const Texture& Font::getTexture(unsigned int characterSize) const
{
    return m_pages[characterSize].texture;
}
 

characterSize = 24, still not 100% sure on what is causing this yet

20
General / Vector of custom class, how to populate?
« on: October 09, 2014, 09:26:47 pm »
Hello there

I have myself a custom TextObject class and I have myself a GameState class which has a vector of TextObjects, so basically this is how I am populating my vector

TextObject textObject;
        TextObjects.push_back(textObject);
        TextObjects.push_back(textObject);
        TextObjects.at(0).Setup(200, 350, "Score : " + std::to_string(Score), 24, fontLoader.Fonts[FontNames::Main_Font]);
        TextObjects.at(0).CentreText();
        TextObjects.at(1).Setup(200, 100, "Game Over", 48, fontLoader.Fonts[FontNames::Main_Font]);
        TextObjects.at(1).SetRotationAnimation(-12, 12, 0.1);
       

To me this doesn't look quite right but it does work, but I have myself in a pickle, sometimes the player may get a new highscore and if they do I want to show some more text, so I have my code like so

TextObject textObject;
        TextObjects.push_back(textObject);
        TextObjects.push_back(textObject);
        TextObjects.at(0).Setup(200, 350, "Score : " + std::to_string(Score), 24, fontLoader.Fonts[FontNames::Main_Font]);
        TextObjects.at(0).CentreText();
        TextObjects.at(1).Setup(200, 100, "Game Over", 48, fontLoader.Fonts[FontNames::Main_Font]);
        TextObjects.at(1).SetRotationAnimation(-12, 12, 0.1);
       
        if (highScore == true)
        {
                TextObject txtObject;
                TextObjects.push_back(txtObject);
                TextObjects.at(2).Setup(200, 3, "New high score!", 24, fontLoader.Fonts[FontNames::Main_Font]);
        }

But when I go to render my third text item in my vector I get this error "Unhandled exception at 0x5361DCE2 (sfml-graphics-d-2.dll) in SpaceDestroyer.exe: 0xC0000005: Access violation reading location 0xCDCDCDD0." in xtree "_Nodeptr _Pnode = _Root();"

My TextObject has a standard constructor so even If I try

if (highScore == true)
        {
                TextObjects.push_back(TextObject());
                TextObjects.at(2).Setup(200, 3, "New high score!", 24, fontLoader.Fonts[FontNames::Main_Font]);
        }

I still get the error, what is the correct way for my to populate my vector? or is there a better way to store objects than using a vector?

21
Audio / Internal OpenAL error
« on: October 08, 2014, 10:29:21 pm »
Hello there,

I have myself a SoundLoader class which loads some wav files into a map of soundbuffers and then I can call a method called PlaySound which takes an enum to play the sound, here is my method

if (playingSounds.size() == 0)
        {
                playingSounds.push_back(sf::Sound());
                playingSounds.at(0).setBuffer(Sounds[soundName]);
                playingSounds.at(0).play();
        }
        else
        {
                int location = -1;
                for (int i = 0; i < playingSounds.size(); i++)
                {
                        if (!playingSounds.at(i).getStatus() == sf::Sound::Playing)
                        {
                                location = i;
                        }
                       
                        if (location != -1)
                        {
                                break;
                        }
                }

                if (location != -1)
                {
                        playingSounds.at(location).setBuffer(Sounds[soundName]);
                        playingSounds.at(location).play();
                }
                else
                {
                        playingSounds.push_back(sf::Sound());
                        playingSounds.at(playingSounds.size()-1).setBuffer(Sounds[soundName]);
                        playingSounds.at(playingSounds.size() - 1).play();
                }

        }

However I was testing my game and for a minute or so it is all going fine, but then all of a sudden I got this error

An internal OpenAL call failed in SoundSource.cpp (181) : AL_INVALID_NAME, an unacceptable name has been specified

What am I doing to cause this? P.S. my soundloader only has 60 lines of code so not sure what the 181 is relating to

UPDATE

I have just coded my enemy to spam its firing which plays one sound, this worked for a few seconds then an error was shown and my game continued for about 3 seconds then I got this error in visual studio

Unhandled exception at 0x56455365 (sfml-system-d-2.dll) in SpaceDestroyer.exe: 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x07B52FFC).

then it shows in Err.cpp

virtual int overflow(int character)
    {
        if ((character != EOF) && (pptr() != epptr()))
        {
            // Valid character
            return sputc(static_cast<char>(character));
        }
        else if (character != EOF)
        {
            // Not enough space in the buffer: synchronize output and try again
            sync();
            return overflow(character);
        }
        else
        {
            // Invalid character: synchronize output
            return sync();
        }
    }
 

sfml-system-d-2.dll!`anonymous namespace'::DefaultErrStreamBuf::overflow(int character) Line 61 C++

UPDATE

It's ok I found it, I just changed my line to check location to be this

if (playingSounds.at(i).getStatus() != sf::Sound::Playing && location == -1)
                        {
                                location = i;
                        }

but just to let everyone else know, make sure when you are playing sounds never reach more than 140 sounds in memory, this will throw the "Internal OpenAL error", this error happened to me when my playingSounds.size() was equal to 140

22
General / Re: mouse button released is spammed?
« on: October 06, 2014, 11:06:20 pm »
Cheers guys, Not sure why I was checking outside of the pollevent, seems quite stupid if you ask me :P

23
General / mouse button released is spammed?
« on: October 06, 2014, 10:38:28 pm »
I am using SFML and C++ and I am getting an odd problem,

Here is my main game update method

   
while (renderService.Window.isOpen())
        {
                //Poll events
                sf::Event event;
                while (renderService.Window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                renderService.Window.close();
                        running = false;
                }

                MouseMovment(event);
                MouseClick(event);
                Update();
                Draw();
        }

and here is my MouseClick method

   
void Game::MouseClick(sf::Event event)
    {
        sf::Vector2i position = sf::Mouse::getPosition(renderService.Window);
       
            if (event.mouseButton.button == sf::Mouse::Left && event.type == sf::Event::MouseButtonReleased)
            {
                    std::cout << "Mouse released" << std::endl;
        }
    }

now here is the weird part, in my console sometimes my cout will be spammed like 10/20 times, but sometimes it will work perfectly, am I calling the event wrongly?

24
Can this be removed please I know the problem, I wasn't passing my sf::Texture as a reference to my enemy, passing a reference works 100%

25
Hello there,

Ok bare with me on this one, I have myself a class which is a ImageLoader which basically storing a map of sf::Texture, I also have a class which is LevelState.

Now on the construction of my LevelState it required that two references are initialized on the constructor like so

Header snippet

...
public:
LevelState(FontLoader& fontLoader, ImageLoader& imageLoader);
...
private:

        ImageLoader& _imageLoader;
        FontLoader& _fontLoader;
...

Class snippet
LevelState::LevelState(FontLoader& fontLoader, ImageLoader& imageLoader) : _fontLoader(fontLoader), _imageLoader(imageLoader)
{

}

now this is working fine however it only works for some of my classes, I have a method called Setup which uses my imageLoader like so

UISprite.setTexture(imageLoader.Images[ImageNames::L_UI]);
 

and when asking to draw that it renders perfectly. however it doesn't work for my other class.

Here is a class that works fine

LevelState::Setup
DisplayObject displayObject;
        DisplayObjects.push_back(displayObject);
        DisplayObjects.at(0).Setup(0, 0, imageLoader.Images[ImageNames::Background1]);

//SETUP METHOD FOR DisplayObject
void DisplayObject::Setup(int xSet, int ySet, sf::Texture& spriteSet)
{
        XPos = xSet;
        YPos = ySet;
        Sprite.setTexture(spriteSet);
        Sprite.setPosition(XPos, YPos);
}
//
 

My DisplayObject renders without a problem

My Enemy class however is always a white block

Enemy enemy;
                Row1Enemies.push_back(enemy);
                Row1Enemies[0].Setup(75, 25, _imageLoader.Images[ImageNames::L_Enemy2], EnemyType::Shooter, 0.3, 1);

//ENEMY SETUP METHOD
void Enemy::Setup(int xSet, int ySet, sf::Texture spriteSet, EnemyType type, int fireChance, int lifeSet)
{
        XPos = xSet;
        YPos = ySet;
        Sprite.setTexture(spriteSet);
        Type = type;
        Status = true;
        FireChance = fireChance;
        Life = lifeSet;
}
//
 

Why on earth does my displayobject render fine and my enemy doesn't? Can anyone help me out here?

26
General / Re: xtree error when trying to draw sf::Text
« on: September 26, 2014, 01:19:52 am »
its ok, I have changed my code to create a new textObject and push that into my vector and then set the values then, before I was creating a textobject, setting values and then pushing it back.

27
General / xtree error when trying to draw sf::Text
« on: September 26, 2014, 12:58:13 am »
Hello there

I have myself a class that deals with loading fonts like so

#include "FontLoader.h"

FontLoader::FontLoader()
{

}

FontLoader::~FontLoader()
{

}

void FontLoader::LoadFont(GameStates state)
{
        //Load fonts
        Fonts[FontNames::Main_Font].loadFromFile("Assets/Fonts/Font.otf");
}

I then have a MainMenuState which is given a reference of my FontLoader like so

#include "MainMenuState.h"

MainMenuState::MainMenuState()
{

}

MainMenuState::~MainMenuState()
{

}

void MainMenuState::Setup(ImageLoader& imageLoaderSet, FontLoader& fontLoaderSet)
{
        TextObject textObject;
        textObject.Setup(200, 100, "Space Destroyer", 48, fontLoaderSet.Fonts[Main_Font]);
        textObject.SetRotationAnimation(-12, 12, 0.1);
        TextObjects.push_back(textObject);
}

This seems to write fine, but when I hit my draw function

void State::DrawTextObjects(sf::RenderWindow& window)
{
        for (int i = 0; i < TextObjects.size(); i++)
        {
                TextObjects.at(i).Draw(window);
        }
}

I get an error in Unhandled exception at 0x6292DCE2 (sfml-graphics-d-2.dll) in SpaceDestroyer.exe: 0xC0000005: Access violation reading location 0xCCCCCCD0, at

_Nodeptr _Pnode = _Root();

Am I passing the sf::Font the incorrect way?

28
General / Best way to detect Mario style collision?
« on: July 07, 2014, 10:31:07 pm »
Hello there people,

At the moment I'm developing a very simple Mario clone for fun, but I have hit a wall ( you may say :D ). At the moment I'm trying to figure out the best way to detect collision on the ground or a wall, at the moment I'm just checking a rectangle is colliding with another rectangle. Here is the basic code I use to check for collisions

bool CollisionService::PointInRect(int pnt_x, int pnt_y, int rect_x, int rect_y, int rect_w, int rect_h)
{
        if ((pnt_x >= rect_x) && (pnt_x <= rect_x + rect_w - 1))
        {
                if ((pnt_y >= rect_y) && (pnt_y <= rect_y + rect_h - 1))
                {
                        return true;
                }
        }
        return false;
}

bool CollisionService::RectToRectCollision(int rect1_x, int rect1_y, int rect1_w, int rect1_h, int rect2_x, int rect2_y, int rect2_w, int rect2_h)
{
        // top-left cornerif ( pointInRect(rect1_x, rect1_y, rect2_x, rect2_y, rect2_w, rect2_h) ){return true;}
        // top-right corner
        if (PointInRect(rect1_x + rect1_w - 1, rect1_y, rect2_x, rect2_y, rect2_w, rect2_h)){ return true; }
        // bottom-right corner
        if (PointInRect(rect1_x + rect1_w - 1, rect1_y + rect1_h - 1, rect2_x, rect2_y, rect2_w, rect2_h)){ return true; }
        // bottom-left corner
        if (PointInRect(rect1_x, rect1_y + rect1_h - 1, rect2_x, rect2_y, rect2_w, rect2_h)){ return true; }
        // Check to see if rectangle 2 is hit any part of rectanlge 1
        // top-left corner
        if (PointInRect(rect2_x, rect2_y, rect1_x, rect1_y, rect1_w, rect1_h)){ return true; }
        // top-right corner
        if (PointInRect(rect2_x + rect2_w - 1, rect2_y, rect1_x, rect1_y, rect1_w, rect1_h)){ return true; }
        // bottom-right corner
        if (PointInRect(rect2_x + rect2_w - 1, rect2_y + rect2_h - 1, rect1_x, rect1_y, rect1_w, rect1_h)){ return true; }
        // bottom-left corner
        if (PointInRect(rect2_x, rect2_y + rect2_h - 1, rect1_x, rect1_y, rect1_w, rect1_h)){ return true; }
        // If there is no collision
        return false;
}

Now what I am thinking is, I have two rectangles, one which is the characters height but has 2 pixels less width so this can be checked against falling and another rectangle which has 2 less height so it can be checked against walking left and right into a block. Check my first image to see what I mean by the two rectangle solution. If anyone has a better way of checking for collisions or over all a way better solution please let me know as I do feel my solution isn't a great one.

29
Graphics / Re: Odd values for a sprite I'm trying to display
« on: July 01, 2014, 11:13:38 pm »
Ixrec I will definitely take a look at that as that will make it so much easier for me to develop :D

30
Graphics / Re: Odd values for a sprite I'm trying to display
« on: July 01, 2014, 10:21:14 pm »
I found my answer,

If you take a look at my old code

std::vector<DisplayObject*> Level::GetLevelBlockDisplayObjects()
{
    std::vector<DisplayObject*> tempBlocks;

    for (auto block : blocks)
    {
        tempBlocks.push_back(&block.displayObject);
    }

    return tempBlocks;
}

I wasn't declaring auto as a reference, so it was creating an object, getting a reference of that object and then deleteing that object as it went out of scope. This was my bad, so I just changed my auto to be an auto& reference and it works fine now.

std::vector<DisplayObject*> Level::GetLevelBlockDisplayObjects()
{
        std::vector<DisplayObject*> tempBlocks;

        for (auto& block : blocks)
        {
                tempBlocks.push_back(block.GetDisplayObject());
        }

        return tempBlocks;
}

Pages: 1 [2] 3 4 ... 8
anything