Creation of 2d static borders

Started by
4 comments, last by SteveHatcher 10 years, 4 months ago

Hi Guys,

I would like to make a static 2d world compromised of just some borders. I will add in a cube and have it so you can move the cube around but not outside the borders (simple collision detection). The simple idea is pictured below:

8soWhpY.png

My questions are:

- To make the barriers, is that just a collection of vertexes put in the game world?

- I have some books on collision detection to get stuck into, but is the simple gist: if the world space location of a cubes vertex and the barriers vertex overlap... do not move?

- I am a bit stuck on how to make the world periodic (if the cube goes through the top it comes out up through the bottom). Any suggestions or resources on this would be great. (eg. I wonder how you render if half of the cube is top and half is comming up from the bottom).

One last slightly random question. I am still very new to this so it may seem odd, but is there a "vertex generator" where I can draw a simple model and it will spit out the object space vertices required to make this. Eg If I specify 3 vertices to mimic a triangle I have drawn it spits out something like:


Vertex(0.0f, 0.5f, 0.5f),
Vertex(0.5f, -0.5f, 0.5f),
Vertex(-0.5f, -0.5f, 0.5f)

Thanks

Advertisement

There are a couple ways you could draw the borders:

  • Use either two vertex buffers or a single buffer and two draw calls with appropriate starting indexes to draw the border vertices as a LineStrip.
  • Use a single vertex buffer containing all the vertices, create an index buffer to draw the vertices in pairs and render as a LineList.

For the intersections, you'll want to look into line-box intersection tests. Off the top of my head, I would say you could treat the inner edges of the border as a set of planes, intersect the box with each plane, and then check that the intersection point lies on the segment of the plane that is part of the barrier.

To make the cube wrap top-to-bottom as you are describing, you will probably need to draw it twice, once with the center offset just off the top of the screen and once with the center just below the bottom of the screen. Unrelated to the rendering, you will probably want to simply wrap the cube center to the bounds of the sceen, so that if it goes off the bottom, it is logically positioned at the top of the world, even though you may need to draw it in both places for a few frames.

I don't know about your last question, but you could probably write some simple code that could read in something like an ASCII art representation of your level and generate the necessary vertices.

Eric Richards

SlimDX tutorials - http://www.richardssoftware.net/

Twitter - @EricRichards22

Thank you very much, I will take your advice and get started and post the results if I get it working!

Well as per your advice I have a very simple skeleton working:

xXVFPUi.png

So thanks!. You can move the square with the arrow keys and it slides around. Now my next task is to add collision detection in so it wont fly right over the walls. I have been searching online all day and am very confused as to the actual implementation of it. I hear of bounding boxes being the simplest, but I am rather confused.

Do I first need to calculate the bounding box for all my objects. And if so, how do I go about letting the box know that my walls have that shape, wont it put a huge Rectangle over my whole wall as pictured:

WfGqvOo.png

The red border would be the theoretical bounding box. Thanks for your help.

Edit - Ok so I'm taking it back a notch first. I want to just keep the square inside the game world (which is 800 x 800 pixels). So In my head I think I am quite clear on how to do it. During the update loop, something like (for just x axis for now...):


    if (SquarePosition.x > Width - SquareWidth) 
        SquarePosition.x = Width - SquareWidth;  
    else if (SquarePosition.x < 0)                   
        SquarePosition.x = 0;   

The problem is I am not sure how to extract the .x positions,. Right now I am not using objects. I find it much easier to follow and understand code if its all dumped in one page with just function prototypes doing all the work. So my square goes through the following procedure when I move it around with the arrow keys:


	d3d11DevCon->IASetVertexBuffers(0, 1, &triangleVertBuffer, &stride, &offset);
	//Draw the triangle
	XMMATRIX triangleWorld = XMMatrixIdentity();
	Translation = XMMatrixTranslation(fX, fY, 0.0f);
	triangleWorld = Translation;
	WVP = triangleWorld * camView * camProjection;
	cbPerObj.WVP = XMMatrixTranspose(WVP);
	d3d11DevCon->UpdateSubresource(cbPerObjectBuffer, 0, NULL, &cbPerObj, 0, 0);
	d3d11DevCon->VSSetConstantBuffers(0, 1, &cbPerObjectBuffer);
	d3d11DevCon->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
	d3d11DevCon->Draw(4, 0);

Ignore the variable name triangle, its actually a square lol. Any advice on how I Implement this simple bounding game world detection would be great. Thanks.

I've haven't used DX11 yet, but here are some basic principles ...

Generally, you first do bounds detection and then follow it up with a polygon detection. You can use general ray firing to determine general intersection.

If you are only worried about squares, you can do mid-point and line intersections check. If the shapes are more complex, you can check if the square lies in the polygon though line-intersection algorithms (http://www.math.niu.edu/~rusin/known-math/95/line_segs) and the odd-even rule. Take the mid-point of the square and fire a ray in a direction: if the intersections are even the square is outside of the polygon.

Thanks. Right now I am going to just make another black square that is static and do some bounding box stuff on that. Right now because the 'walls' are made of that complex shape I could make it simpler by just making them 3 rectangles each with their own bounding box.

I am just not quite sure how to get started. Is the bounding box an ID3D11Buffer type? Is it part of DirectX or just C++?

My biggest problem is getting started on stuff like this, I really need some working code that I can compile and examine but have not been able to find any simple samples that have just the bare minimum.

Thanks

This topic is closed to new replies.

Advertisement