Axis Aligned Rectangles

So, what are you looking at in that clip?

Exactly what the title of this post is eluding to, a Rectangle structure that can be used to detect collision like the one we have in XNA. I have kept it Axis Aligned just like the intrinsic XNA one. If you don’t know what I mean by Axis Aligned, then have a Google or a Bing for it, it basically means that the collision detection is done based on the rectangle not being rotated.

In the clip we see RED rectangles, these show up when two rectangles intersect, a BLUE rectangle is the Union rectangle that holds both of the colliding rectangles, the GREEN rectangle is shown when a rectangle is Contained by another and the WHITE rectangle is the Intersect rectangle.

I have been working on trying to get a C++ framework together so I can start writing about some GPU elements, ad realize that I am skimping on the code snippets, so, here is how I have constructed the RCRectangle structure.

My header file looks like this:

struct RCRectangle
{
protected:
public:

    float X;
    float Y;
    float Width;
    float Height;

    DECLDIR RCRectangle(void);
    DECLDIR RCRectangle(XMFLOAT4 dimensions);
    DECLDIR virtual ~RCRectangle(void);


    DECLDIR static RCRectangle Intersect(RCRectangle rectangle1, RCRectangle rectangle2);
    DECLDIR static void Intersect(RCRectangle rectangle1, RCRectangle rectangle2, OUT RCRectangle rectangle3);

    DECLDIR virtual bool Intersects(RCRectangle rectangle);

    DECLDIR virtual bool Contains(RCRectangle rectangle);

    DECLDIR static RCRectangle Union(RCRectangle rectangle1, RCRectangle rectangle2);
    DECLDIR static void Union(RCRectangle rectangle1,RCRectangle rectangle2, OUT RCRectangle rectangle3);
    
};

So, we have an X and Y to give us our top left corner and a Width and a Height to give us the opposing corner.

I have also put in the same methods we have in XNA, so Intersect to get the rectangle where the two rectangles overlap, Intersects to detect rectangle intersection, Contains for when a rectangle contains another and Union used to create a rectangle that will contain the two rectangles.

The RCRectangle.cpp file has the function bodies and they look like this:

RCRectangle::RCRectangle(void)
{

}

RCRectangle::RCRectangle(XMFLOAT4  dimensions)
{
    X = dimensions.x;
    Y = dimensions.y;
    Width = dimensions.z;
    Height = dimensions.w;
}

RCRectangle::~RCRectangle(void)
{

}

void RCRectangle::Intersect(RCRectangle rectangle1, RCRectangle rectangle2,OUT RCRectangle rectangleOut)
{
    if(rectangle1.Intersects(rectangle2))
    {
        float x,y,w,h = 0;

        if(rectangle1.X >= rectangle2.X)
            x = rectangle1.X;
        else
            x = rectangle2.X;

        if(rectangle1.Y >= rectangle2.Y)
            y = rectangle1.Y;
        else
            y = rectangle2.Y;

        if(rectangle1.X + rectangle1.Width <= rectangle2.X + rectangle2.Width)
            w = (rectangle1.X + rectangle1.Width) - x;
        else
            w = (rectangle2.X + rectangle2.Width) - x;

        if(rectangle1.Y + rectangle1.Height <= rectangle2.Y + rectangle2.Height)
            h = (rectangle1.Y + rectangle1.Height) - y;
        else
            h = (rectangle2.Y + rectangle2.Height) - y;

        rectangleOut = RCRectangle(XMFLOAT4(x,y,w,h));
    }
}
RCRectangle RCRectangle::Intersect(RCRectangle rectangle1, RCRectangle rectangle2)
{
    RCRectangle retVal(XMFLOAT4(0,0,0,0));

    if(rectangle1.Intersects(rectangle2))
    {
        float x,y,w,h = 0;

        if(rectangle1.X >= rectangle2.X)
            x = rectangle1.X;
        else
            x = rectangle2.X;

        if(rectangle1.Y >= rectangle2.Y)
            y = rectangle1.Y;
        else
            y = rectangle2.Y;

        if(rectangle1.X + rectangle1.Width <= rectangle2.X + rectangle2.Width)
            w = (rectangle1.X + rectangle1.Width) - x;
        else
            w = (rectangle2.X + rectangle2.Width) - x;

        if(rectangle1.Y + rectangle1.Height <= rectangle2.Y + rectangle2.Height)
            h = (rectangle1.Y + rectangle1.Height) - y;
        else
            h = (rectangle2.Y + rectangle2.Height) - y;

        retVal = RCRectangle(XMFLOAT4(x,y,w,h));
    }

    return retVal;
}

bool RCRectangle::Intersects(RCRectangle rectangle)
{
    bool retVal = true;

    // AA Check.
    if(
        X > rectangle.X + rectangle.Width ||
        Y > rectangle.Y + rectangle.Height ||
        X + Width < rectangle.X ||
        Y + Height < rectangle.Y)
    {
        // Can't possibly have overlap
        retVal = false;
    }
    else
    {
        retVal = true;
    }


    return retVal;
}

bool RCRectangle::Contains(RCRectangle rectangle)
{
    bool retVal = false;

    if(X <= rectangle.X && X + Width >= rectangle.X + rectangle.Width &&
        Y <= rectangle.Y && Y + Height >= rectangle.Y + rectangle.Height)
        retVal = true;

    return retVal;
}

RCRectangle RCRectangle::Union(RCRectangle rectangle1,RCRectangle rectangle2)
{
    float x = 0 ,y = 0,w = 0,h = 0;

    if(rectangle1.X <= rectangle2.X)
        x = rectangle1.X;
    else
        x = rectangle2.X;

    if(rectangle1.Y <= rectangle2.Y)
        y = rectangle1.Y;
    else
        y = rectangle2.Y;

    if(rectangle1.X + rectangle1.Width >= rectangle2.X + rectangle2.Width)
        w = (rectangle1.X + rectangle1.Width) - x;
    else
        w = (rectangle2.X + rectangle2.Width) - x;

    if(rectangle1.Y + rectangle1.Height >= rectangle2.Y + rectangle2.Height)
        h = (rectangle1.Y + rectangle1.Height) - y;
    else
        h = (rectangle2.Y + rectangle2.Height) - y;

    return RCRectangle(XMFLOAT4(x,y,w,h));
}

void RCRectangle::Union(RCRectangle rectangle1,RCRectangle rectangle2, OUT RCRectangle rectangle3)
{
    float x = 0 ,y = 0,w = 0,h = 0;

    if(rectangle1.X <= rectangle2.X)
        x = rectangle1.X;
    else
        x = rectangle2.X;

    if(rectangle1.Y <= rectangle2.Y)
        y = rectangle1.Y;
    else
        y = rectangle2.Y;

    if(rectangle1.X + rectangle1.Width >= rectangle2.X + rectangle2.Width)
        w = (rectangle1.X + rectangle1.Width) - x;
    else
        w = (rectangle2.X + rectangle2.Width) - x;

    if(rectangle1.Y + rectangle1.Height >= rectangle2.Y + rectangle2.Height)
        h = (rectangle1.Y + rectangle1.Height) - y;
    else
        h = (rectangle2.Y + rectangle2.Height) - y;

    rectangle3 = RCRectangle(XMFLOAT4(x,y,w,h));
}

 

If you are wondering what the DELDIR is, it’s a macro I am using so the methods can be accessed from out side the library when included in a project and it looks like this:

#if defined DLL_EXPORT
#define DECLDIR __declspec(dllexport)
#else
#define DECLDIR __declspec(dllimport)
#endif

Hope you find this post useful, as ever C&C are welcome :D

Render Targets

So, done a bit more on my library, not only do I have a working SpriteBatch class, I have also given the option to be rendered instanced :), so once I got that all working, I decided the next thing for me to look at are Render Targets.

As you can see from the clip, I have it working, but there is an issue in there that I want to get sorted before I move on, I am not totally sure why it’s doing what it’s doing, but I hope to get to the bottom of it..

Good news for me, is once I have this sorted I can look at post processing and deferred lighting, which if you have followed any of my posts in the past, I love playing with both those elements.

XNA Samples in C++

So I was looking at one of the 2D samples on the creators club site, namely this one, where MS show how to use XNA to create a quick 2D shooter. I thought this would be a good test to see how far I am with my library as far as my sprite batch code goes, and hoped it would show up a few issues (which thankfully it did)

Now, due to my poor C++ I still have a number of issues, so I wont be putting this library up yet, but I plan to take take the existing creators club samples and use them and their assets (hope MS don’t mind) to do samples for my library.

I am also hoping to get working samples for Windows 8 Metro applications too, time willing.

In the mean time, take a look at where I am so far with this first sample. As you can see in the clip I have animated sprites now as well, still no sound, music is played by the clip and is not in game music, but that’s just another on the TODO list :P All assets in the clip are rendered using my C++ library, but all the assets are from the XNA sample as is.

As ever your comments are more than welcome :)

RandomchaosDX11Lib & SpriteBatch

 

So, since the last post, I have started to create a XNA like library for me to use while learning all this new gubbins. I know Shawn has already started a DXToolKit, and I have to say I have lifted a tone of code from it to get my library working, but to get it working in what I had already written was proving to be difficult due to my total lack of C++ knowledge, but as this is for my experiments, it won’t hurt none me writing my own and learning from Shawn along the way.

So, as it stands at the moment, I have wrapped all I have covered so far in the library and you can now create a derived instance of RCGame (similar to our Game class in XNA) and start adding stuff to the Components list and now draw 2D using the SpriteBatch.

Some of you will be happy I am looking at the 2D stuff, it’s still a long way off done, and I am sure won’t be as robust as Shawn's stuff, but I am learning from it, and hope you guys can, as ever benefit from what I am picking up (or not in some cases :P)

Putting it all together

So, we have a pretty simple framework now for defining objects in our game world, but we really need a class for things we want to render in the world, so we will create the RC3DBaseDrawableObject class, as before, create a class and call it RC3DBaseDrawableObject and set it’s base class as IDrawable

image

We also want this object to be updatable and have all the goodies we have in our RC3DBaseObject, so we will also derive from that class, you RC3DBaseDrawableObject.h file should look like this now

#pragma once
#include "interfaces.h"
#include "RC3DBaseObject.h"

class RC3DBaseDrawableObject : public RC3DBaseObjectpublic IDrawable
{
public:
RC3DBaseDrawableObject(void);
~RC3DBaseDrawableObject(void);
};

Read more: Putting it all together

You are here: Home C++ DirectX Getting Started