Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

2D All the way! / How to draw a rotated box

Author
Message
monotonic
18
Years of Service
User Offline
Joined: 24th Mar 2006
Location: Nottinghamshire, England
Posted: 2nd Aug 2009 19:51
Hi folks,

I've been trying to figure out how to draw a 2D box that will be rotated. I need to be able to draw it pixel by pixel so I can't use sprites or anything like that. I'm not actually drawing to screen I'm drawing some values into a 2D array but it's basically the same thing, because images and screen buffers are basically 2D arrays.

The code I have currently to calculate the position of values (rotated about the origin 0, 0)



but this doesn't work very well, any ideas?

Thanks in advance

01100001 01101110 01101111 01110100 01101000 01100101 01110010
01110000 01101111 01101001 01101110 01110100 01101100 01100101 01110011
01110011 01110011 01101001 01100111
Pincho Paxton
21
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 3rd Aug 2009 15:25 Edited at: 3rd Aug 2009 15:27
Yeah.. my idea is to make the box 3D

Not really a joke, but a 2D game can be made in 3D, and the code is almost the same. I made a breakout game in 2D, and then changed it to 3D, and found out that there was hardly anything to change at all. Just put your images on plains, and your 2D is now 3D.

monotonic
18
Years of Service
User Offline
Joined: 24th Mar 2006
Location: Nottinghamshire, England
Posted: 3rd Aug 2009 15:41
Hi,

yeah my game is 3D, I'm currently in the process of implementing an ai pathfinding system. What I have is a 2D array which represents the nav cells used for my A* algorithm. Now what I do in the world editor is draw a 3D box around the geometry/obstacle and assign a property to specify that it is indeed an ai obstacle. Then in my world process function I search for these ai obstacle boxes and effectively draw them into the ai data array. Now for boxes with a rotation of zero it's easy, you simply transform world coordinates into integer array indices and draw thevbox. However when the box is rotated it becomes a little more problematic. I have got some code to work using the above snippet, but it is very flakey for instance I get holes in the middle of the box (in the array that is) but I think this is due to floating point to integer rounding errors.

01100001 01101110 01101111 01110100 01101000 01100101 01110010
01110000 01101111 01101001 01101110 01110100 01101100 01100101 01110011
01110011 01110011 01101001 01100111
Pincho Paxton
21
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 3rd Aug 2009 16:11 Edited at: 3rd Aug 2009 16:20
You could make an editor on a bitmap that has boxes pasted down taken from your mapping system. Then part of the editor takes the colour pixels from the screen coordinates, and stores the data. Now you no longer need the editor, because you have the data to work with. A sort of setup system. For the setup just use rotated sprites, they are only temporary anyway. For an unrotated box have a special number like 2, to save data. The rest is perfect pixel collision.

monotonic
18
Years of Service
User Offline
Joined: 24th Mar 2006
Location: Nottinghamshire, England
Posted: 3rd Aug 2009 16:58
Yeah I've thought about doing something similar to that by rendering my world from a top down view with only the obstacle nodes showing and everything else hidden. Then just use the data from the buffer which I rendered to. I'm thinking about changing my pathfinding system to use a navmesh, which will be generated at runtime then saved out for use in later loading. So I could preprocess my world and save out the navmesh, then just pack up the files when I deploy the game.

I've been reading through this document http://web.media.mit.edu/~jorkin/aiide05OrkinJ.pdf and it's given me all kinds of ideas for implementing an AI system. I think that a navmesh is going to be a more efficient method, although it is more complicated and will take longer, I'm going to have to weigh the pros and cons.

01100001 01101110 01101111 01110100 01101000 01100101 01110010
01110000 01101111 01101001 01101110 01110100 01101100 01100101 01110011
01110011 01110011 01101001 01100111
Van B
Moderator
21
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 4th Aug 2009 16:44
Why not make a sprite?

Grab some colour in an image, if it's solid colour then size is barely important, 8x8 should be plenty. Then size the sprite to suit the width and length, and rotate it to suit the angle.

There you have a completely controllable rotated box/rectangle that you can alpha fade, and if you use paste sprite, you could easily use the same sprite to draw all your rotated boxes and that can bring it back to 2D, I think all you need to do is disable backsave on a sprite to let it be pure 2D again.

Moreover, you could use any shape, as if you use sprites you can just draw the shapes, then rotate and size them with code however you like.

Sprites are lovely, I use them for all sorts of things, especially for showing particular parts of an image using those UV coordinate adjustments, or even giving a real fast zoom mode.


Health, Ammo, and bacon and eggs!
monotonic
18
Years of Service
User Offline
Joined: 24th Mar 2006
Location: Nottinghamshire, England
Posted: 6th Aug 2009 17:46
Yeah sprites are pretty handy for all kinds of stuff, I never thought of using them for zooming/scaling good thinking. I've decided to go for another method of pathfinding, I read a paper that some guy wrote for the GDC it uses a flood fill method for mapping the obstacles for use in a 3D game world..

01100001 01101110 01101111 01110100 01101000 01100101 01110010
01110000 01101111 01101001 01101110 01110100 01101100 01100101 01110011
01110011 01110011 01101001 01100111
Van B
Moderator
21
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 6th Aug 2009 19:01
Yeah, that's what I'd do - make a grid and flag each part that has an obstacle. I did have an example of that, could upload the code if you like - works incredibly well and has the added benefit that if enemies are going to the same location, they actually share the work as their fill passes accumulate.

You should check out my codes, it's very minimalist but effective if I do say so myself



Use the arrow keys to move the yellow box, starts at the top left - then press space and all the nodes will find the yellow box. Nodes that are green are moving, yellow means thinking, and red means stopped. There are tweaks that I would do to it if I had the time, like storing a value to represent the number of moves in each grid - so the destination might start at 1, then adjoining cells would be 2, 3, 4 - but the thing is that when a cell has more than one option for the path, it could use the lowest value. This would make it follow the very shortest path instead of the first workable path it finds.


Health, Ammo, and bacon and eggs!
AndrewT
17
Years of Service
User Offline
Joined: 11th Feb 2007
Location: MI, USA
Posted: 9th Aug 2009 00:32 Edited at: 9th Aug 2009 00:35
If you're still in need of rotated boxes, here's what I came up with:



Just call RotBox_Init() at the top of your code, passing a free image number and a free sprite number, then call RotBox_Draw() whenever you want to draw one. It uses sprites, but because the backsave is set to 0 and Paste Sprite is used, the box will draw as if it was drawn using the 2D commands, i.e. you'll have to CLS if you want to clear them just like regular boxes made using Box.

EDIT:

Ooops, probably shoulda read the first post. Well hopefully this will come in handy for somebody anyway.

i like orange
Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 30th Aug 2009 01:56 Edited at: 30th Aug 2009 01:57
I wrote this a while ago, it will let you draw any regular polygon at any angle.


Haven't tested the example but function should be fine.

TGC Forum - converting error messages into sarcasm since 2002.

Login to post a reply

Server time is: 2024-04-24 16:46:40
Your offset time is: 2024-04-24 16:46:40