Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!

We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Member Since 14 Dec 2013
Offline Last Active Today, 07:10 AM

Topics I've Started

Culling points from a polygon.

28 August 2014 - 10:01 PM

Hello everyone,


i am currently experiencing a problem figuring an algorithm to remove unneeded points from a polygon structure. Normally i wouldn't consider posting here, but this one got me stumped for quite a while.


So here is the problem:


I have a std::vector of a Point structure that form a convexe polygon.

Somes of the points in the vector may be not needed and could be removed without changing the shape of the polygon.

I need to figure out how to remove thoses unneeded points.

I have attached a picture representating this array of points.
As you can see, two point can be removed without changing the shape of the polygon (50,0 and 50,50)


Here is more informations:

The polygon will always be contained within a known range (xmin to xmax, ymin to ymax).

A point always have X or Y equals either known min-max value (can be both). So if xmin = 0, xmax = 100, ymin = 0, ymax = 50, the point (25,25) is impossible.

The polygon is not always a simple square or rectangle and can be anything convexe (Trapeze, triangle, losange, etc...).

The array will always be sorted clockwise but the first point is not always 0,0.


Here is what i already tried to solve this problem:

I tried looking for points in the middle of a side and removing them . I think the idea is good, but my logic is faulty, though.
This algorythm work for a simple rectange but fail with anything more complexe, like a trapeze.

void Cull( std::vector<CVector> &_vertices, float _xmin, float _ymin, float _xmax, float _ymax )
    // Work for a simple square or rectangle, but fail for anything else like a trapeze.
        CVector lastvalidx = _vertices[0];
        CVector lastvalidy = _vertices[0];

        for(unsigned i = 1; i < _vertices.size();)
            int n = i + 1 < _vertices.size() ? i + 1 : 0; // next point

            //Remove any point that is in the middle of a plane
            if( ( (_vertices[i].x != lastvalidx.x && (_vertices[i].x != _xmin && _vertices[i].x != _xmax) ) ||
                (_vertices[i].y != lastvalidy.y && (_vertices[i].y != _ymin && _vertices[i].y != _ymax) ) ) )
                //remove the point, since it is not needed.
                _vertices.erase(std::remove(_vertices.begin(), _vertices.end(), _vertices[i]), _vertices.end());
               lastvalidx = _vertices[i];
               lastvalidy = _vertices[i++];


If you would take a look at my problem, i will be thankful. I will continue trying to solve it on my side in the meantime.
I really hope my problem is not too confusing to understand. If more information is needed, i will post it.
As always, thank you very much for your time.

My physic engine is done, but i'm still not satisfied. Physic book related.

02 June 2014 - 11:21 AM

Hello everyone.


On and off for the last 6 month, i've worked on my 2D physic engin directly integrated in the game i'm working on.
I have gone through several iteration, each one being better and better than the last one.


The current build is "acceptable", which mean it have every features that will be in my final game, like polygonal collision detection and resolution, slopes, rotating bounding boxes, skeletal anchors and more. It is by no means perfect. I am aware of a couple bugs here and there.


But i feel i can do better, more optimized or just more robust. I've read a lot of tutorial on the web to achieve everything so far, but i think i've come to a point where i need to read a good 2d physic book to make my engin better and better.


I have done some research to find good books, but everything i find so far have 3d physic as the main focus. The few 2d book i have found don't have a lot of reviews or have somes bogus reviews.


Would you recommend a good 2d physic book?
I am looking for an advanced book.


Thank you very much for your time.

Is this commenting macro safe to use?

22 May 2014 - 10:18 PM

Hello everyone.


I'm using a simple macro to comment out part of my code in release build, but i'm wondering if this could lead to some sort of problem, as i'm trying to cheat the precompiller into adding comments against the rule.


I use this to avoid writing duplicated code.


Basically, here is the macro

#if defined DEBUG
#define MACRO 
#define MACRO );/ ## /

What it does is: If i'm in debug build, it does nothing. If i'm in release build, it close a function and comment the rest of the line.

The "/ ## /" part is needed to trick the compiler into thinking comment dont exist. But they do.


This is how i use the macro. (The code is simplified)

//Initialize a long list of interfaced classes into a map, allowing easy Factory access.
void Bar()
    //Assume each different letters is an interfaced class
    Foo(A0, A1 MACRO, A2, A3, A4);
    Foo(B0, B1 MACRO, B2, B3, B4);
    // Many Foo later...
    Foo(Z0, Z1 MACRO, Z2, Z3, Z4);

#if defined DEBUG
//Types don't really matter. Just the number of parameters is interesting.
void Foo(Type0, Type1, Type2, Type3, Type4)
    // Do something, in debug
void Foo(Type0, Type1)
    // Do something, in release

As you can see, in release build, the compiler will see "Foo(A0, A1 );" as the comment will clear the rest of the line.
This way, i avoid writing this code:

#if defined DEBUG
Foo(A0, A1, A2, A3, A4);
Foo(B0, B1, B2, B3, B4);
// Many Foo later...
Foo(Z0, Z1, Z2, Z3, Z4);
Foo(A0, A1);
Foo(B0, B1);
// Many Foo later...
Foo(Z0, Z1);

The reason i use it is to avoir writing duplicate code to avoid possible Copy/Paste errors.

As of writing this post, the macro work fine in either build, but i have this strange feeling i'm doing something cringe-worthy.


Am i really going overboard just to avoid writing duplicated code?

If so, is there other options?

Create new white texture from existing SDL_Texture.

05 April 2014 - 04:28 PM

Hello everyone,


I'm trying to accomplish something in SDL and i'm wondering if it can be done easily.


What i want to do it this:


First i create a new texture from a PNG file with this code. No problem so far.

bool CTextureInfo::Unserialize(std::ifstream &_inputfilestream)
    int pngsize = 0;
    _inputfilestream.read(reinterpret_cast<char *>(&pngsize), sizeof(int) );
    char *textureBinaries = new char[pngsize];
    _inputfilestream.read(reinterpret_cast<char *>(textureBinaries), pngsize);
    // Create texture
    SDL_RWops *img = SDL_RWFromConstMem(textureBinaries, pngsize);
    // This part is building the texture through SDL_image
    SDL_Texture *texture = IMG_LoadTexture_RW(CSDL_Setup::GetInstance()->GetRenderer(), img, 0);

After this i want to create another texture that will be a copy of the first one, but instead all pixels that are not transparent become white.

See attached image for example of what texture 1 and texture 2 should look like.


However, i am failing really hard to figure how to do this.

I thank you in advance.



There got to be a better way to manage animations.

23 March 2014 - 09:29 PM

Somes weeks ago, i've started pouring more hours on the animation part of my game, which was pretty bare up to this point. After sucessfully creating a tool to easily create and modify my 2d animations from a sprite sheet (setting things like sprite rect, crop, hitboxes, weapon mounting point and angle, etc...), i now have to actually do the logic of switching all of the animation the actors have in the game.


The code below is my function to change the actor's animation based on his state.

While, i've not actually come to a dead end, i feel what my code is starting to feel tangled and harder to debug.

As you will notice a little bit further down, it'll only get more complicated, as the number of possible animation is very wide.


So my question is this: Is there a better way of doing this? Or should i just continue doing all thoses else-if?

I really do not want all the hours i'm doing to go to waste if i have to redo everything, especially all the time spent doing my own animation tool (which is very specific to my game).

void IActor::ManageAnimation()
    Animations id = ANIMATION_IDLE_OOC;
    //If out of combat
    if(!IsState(STATE_INCOMBAT) )
            id = ANIMATION_JUMP_OOC;

        else if(IsState(STATE_FALLING))
            id = ANIMATION_FALL_OOC;

        else if(IsIdle(MAX_IDLE_TIME))
            id = ANIMATION_SLEEP_OOC;

        //Sliding show be shown over moving
        else if(IsState(STATE_SLIDING))
            id = ANIMATION_SLIDE_OOC;

        else if(IsState(STATE_MOVING))
            id = ANIMATION_MOVE_OOC;

enum Animations
    //Generic animation. Everyone should have theses. It's the job of this class to set them.
    //Out of combat
    ANIMATION_IDLE_OOC,            //Basic idle animation out of combat.
    ANIMATION_SLEEP_OOC,        //Animation for idling too long... Player yawn, wolf lay down and sleep, etc...
    ANIMATION_MOVE_OOC,            //Moving
    ANIMATION_LOOK_UP_OOC,        //Look up
    ANIMATION_LOOK_DOWN_OOC,    //Look down
    ANIMATION_TREATHENED_OOC,    //Transition between out of combat and in combat.
    ANIMATION_JUMP_OOC,            //Jump
    ANIMATION_FALL_OOC,            //Falling
    ANIMATION_SLIDE_OOC,        //When the actor is changing direction or sliding down a slope.

    //In combat
    ANIMATION_IDLE_IC,            //Basic idle animation in combat.
    ANIMATION_MOVE_IC,            //Moving
    ANIMATION_LOOK_UP_IC,        //Look up
    ANIMATION_LOOK_DOWN_IC,        //Look down
    ANIMATION_TREATHENED_IC,    //Transition between in combat and out of combat.
    ANIMATION_JUMP_IC,            //Jump
    ANIMATION_FALL_IC,            //Falling
    ANIMATION_SLIDE_IC,            //When the actor is changing direction or sliding down a slope.

    //End of generic animation

    //Specific animation. Not everyone have them. It's the job of the Actor's interface to set them. thoses animation can all be canceled by us.
    //Reserve for physical attack with a weapon or unarmed.

    //Reserved for Spell animations

    //Reserved for social animations. could be laugh, cry, angry, yell, etc...
    //End specific animation