Concerning ingame coordinates representation

Do you need help? Ask here!

Bug reporting belongs here: http://developer.wz2100.net/newticket
Phoenix946
Greenhorn
Posts: 9
Joined: 02 Jun 2009, 16:13

Concerning ingame coordinates representation

Post by Phoenix946 »

First off, my question concerns actual game code, if I'm not meant to ask about that, please just let me know and don't bother answering.

I want to draw something in the game, and after some experimentation it seems to me that the game uses two different representations for coordinates:
- One is linked to the monitor of the user, as in the topleft corner is (0,0) and bottomright corner is whatever your monitor resolution is. This is used for all the methods that have iV_ or pie_ in it.
- The other is linked to the map, where the topleft corner of the map is (0,0) and the bottomright corner is however big the map is. This is used for the coordinates of all objects in the game.

I want to draw something that is linked to the coordinates of the map and the objects. As example, formations for droids are used. These formations contain an x and a y that shows where in the map they are. Now I want to draw a line through this formation that stays with the formation while it moves. I can get the ends of this line in the following way (200 just as an example):

Code: Select all

x_min = (psFormation->x - 200);
x_max = (psFormation->x + 200);
y_min = (psFormation->y - 200);
y_max = (psFormation->y + 200);
My question now, how do I get this line to show in such a way that it stays with the formation? As in, it only moves if the formation also moves. I have not been able to find an example in the code that uses this second representation of the coordinates to actually draw something. Also openGL does not seem to cut it:

Code: Select all

glBegin(GL_LINES);
glVertex2f((float)startX, (float)startY); 
glVertex2f((float)endX, (float)endY);
glEnd( );
This does not show anything. (Note that this is my first time ever using openGL so of course this may just be wrong) If you know how to show a line in this way it would be great, or any example in the code that uses this representation would be nice, that should give me enough of an indication on how it is used that I can figure the rest out myself.

Thanks a lot in advance!

(PS, for those that answered in my previous topic and interested, I stopped trying to get it to work with a Radeon, on a different computer now)
cybersphinx
Inactive
Inactive
Posts: 1695
Joined: 01 Sep 2006, 19:17

Re: Concerning ingame coordinates representation

Post by cybersphinx »

Phoenix946 wrote:I want to draw something in the game, and after some experimentation it seems to me that the game uses two different representations for coordinates:
- One is linked to the monitor of the user, as in the topleft corner is (0,0) and bottomright corner is whatever your monitor resolution is. This is used for all the methods that have iV_ or pie_ in it.
- The other is linked to the map, where the topleft corner of the map is (0,0) and the bottomright corner is however big the map is. This is used for the coordinates of all objects in the game.
Lets call the first one "screen space", the second one "world space". The world space actually has 3 dimensions.
I want to draw something that is linked to the coordinates of the map and the objects. As example, formations for droids are used. These formations contain an x and a y that shows where in the map they are. Now I want to draw a line through this formation that stays with the formation while it moves. I can get the ends of this line in the following way (200 just as an example):
[...]
My question now, how do I get this line to show in such a way that it stays with the formation? As in, it only moves if the formation also moves. I have not been able to find an example in the code that uses this second representation of the coordinates to actually draw something. Also openGL does not seem to cut it:

Code: Select all

glBegin(GL_LINES);
glVertex2f((float)startX, (float)startY); 
glVertex2f((float)endX, (float)endY);
glEnd( );
You're drawing in world space, and need a 3rd coordinate to indicate the height of the object you want to draw. If you omit that, it'll be drawn at height 0, which is probably below everything and thus invisible. So you need to find a suitable height for your line.

Or you could try drawing in screen space, the unit bracket/health bar code could help you there to find out how to translate coordinates in world space to screen space.
User avatar
Zarel
Elite
Elite
Posts: 5770
Joined: 03 Jan 2008, 23:35
Location: Minnesota, USA

Re: Concerning ingame coordinates representation

Post by Zarel »

cybersphinx wrote:You're drawing in world space, and need a 3rd coordinate to indicate the height of the object you want to draw. If you omit that, it'll be drawn at height 0, which is probably below everything and thus invisible. So you need to find a suitable height for your line.

Or you could try drawing in screen space, the unit bracket/health bar code could help you there to find out how to translate coordinates in world space to screen space.
I think there's a function like "what's under my cursor" or something like that, that returns the tile coordinate (2D heightless) under the cursor, that effectively does what he's looking for.
Phoenix946
Greenhorn
Posts: 9
Joined: 02 Jun 2009, 16:13

Re: Concerning ingame coordinates representation

Post by Phoenix946 »

cybersphinx wrote: You're drawing in world space, and need a 3rd coordinate to indicate the height of the object you want to draw. If you omit that, it'll be drawn at height 0, which is probably below everything and thus invisible. So you need to find a suitable height for your line.

Or you could try drawing in screen space, the unit bracket/health bar code could help you there to find out how to translate coordinates in world space to screen space.
Hmm, I found the method mapHeight(x,y) which gave me the height of the map at (x,y) (was around 20 at the starting area) but that did not draw a line with that openGL code. Nor did I see anything when I simply drew a line from (x, y, 0) to (x, y, 100). I even tried with x and y around 200 to see if openGL was also in screen space but nothing showed. So I took your advice on drawing in screen-space, which worked out pretty well, thanks :)
Zarel wrote: I think there's a function like "what's under my cursor" or something like that, that returns the tile coordinate (2D heightless) under the cursor, that effectively does what he's looking for.
I think I found that function, but I couldn't figure out how you had in mind I'd use that :suprised: I took the screenX and screenY from the droids in the formation in the end.

One thing I found weird btw, I don't know if that has been reported before already, but it seems like the list asMembers in the _formation struct does not immediately receive all members when the formation is created. Every time you order droids to move, a new formation is created and asMembers is filled with those droids (in the form of F_MEMBER), but when I loop through asMembers to get the coordinates of all the droids in that formation, I get some NULL values for a couple of seconds before he recognizes (or whatever) the droids and is able to give back the coordinates. Code looks like this:

Code: Select all

F_MEMBER *asMembers = psFormation_gpf->asMembers;
SDWORD unit;
COORDINATE coordinates[psFormation_gpf->refCount]; //COORDINATE contains screenX and screenY in one so that they can be inserted as one into a list.
	
for(unit = 0; unit < psFormation_gpf->refCount; unit++)
		{
			DROID* psDroid = asMembers[unit].psDroid;
			
			if(psDroid != NULL)
			{
				psDroid->coordinate.screenX = psDroid->sDisplay.screenX;
				psDroid->coordinate.screenY = psDroid->sDisplay.screenY;
				coordinates[unit] = psDroid->coordinate;
			}
			else
			{
				printf("%s \n", "No droid!?");
			}
		}
And I'd get No droid!? for a few seconds every time I order droids to move. Any idea what is up with that?
Phoenix946
Greenhorn
Posts: 9
Joined: 02 Jun 2009, 16:13

Re: Concerning ingame coordinates representation

Post by Phoenix946 »

As it turns out, using screen space won't cut it because of the possibilities of rotation and zooming. I need to draw something that is not dependent on changing screen space coordinates: basically I have four world space coordinates that represent the corners of a four-corner formation around a group of tanks, and I want lines drawn between these corners to make the formation visible. These coordinates are dependent on the x and y world space coordinates of the formation. I tried drawing it in screen space, but rotation (and zooming) changes the shape, while I want the shape to be static and rotate along with the screen.

So I was wondering if you could answer to this part of my previous post, which sums up the result of trying what you suggested for the world space:
Phoenix946 wrote: Hmm, I found the method mapHeight(x,y) which gave me the height of the map at (x,y) (was around 20 at the starting area) but that did not draw a line with that openGL code. Nor did I see anything when I simply drew a line from (x, y, 0) to (x, y, 1000). I even tried with x and y around 200 to see if openGL was also in screen space but nothing showed.
Do you have ideas on why it would not show anything in any of the scenarios I tried? As said, I never used openGL before, I read some pieces of info about it here and there and that code I posted was the only code of openGL that I had in the program, am I using that wrongly perhaps?
User avatar
whippersnapper
Regular
Regular
Posts: 1183
Joined: 21 Feb 2007, 15:46

Re: Concerning ingame coordinates representation

Post by whippersnapper »

.

I'm the furthest from an OpenGL guru as you can get but did you happen to have a look HERE in your "I read some pieces of info about it here and there..." ?

Regards, whip O_O
Last edited by whippersnapper on 04 Jul 2009, 16:50, edited 1 time in total.
.
"I need no warrant for being, and no word of sanction upon my being. I am the warrant and the sanction." Anthem

"Art is the selective recreation of reality according to the artist's metaphysical value judgments." A. Rand
.
Per
Warzone 2100 Team Member
Warzone 2100 Team Member
Posts: 3780
Joined: 03 Aug 2006, 19:39

Re: Concerning ingame coordinates representation

Post by Per »

Don't draw with your own primitives. Use the primitive drawing functions in lib/ivis_opengl/ (or lib/ivis_common/) to do this. That way you won't risk trying to draw while the graphics state is not suited for drawing. Also note that x and z denote the horizontal plane, while y gives the vertical plane for drawing, but this is not consistent throughout the codebase at higher levels.
Phoenix946
Greenhorn
Posts: 9
Joined: 02 Jun 2009, 16:13

Re: Concerning ingame coordinates representation

Post by Phoenix946 »

whippersnapper wrote:.

I'm the furthest from an OpenGL guru as you can get but did you happen to have a look HERE in your "I read some pieces of info about it here and there..." ?

Regards, whip O_O
Nope, I hadn't come across that one yet. Looks pretty relevant, albeit scary for a newcomer haha. Thanks for sharing!
Per wrote: Don't draw with your own primitives. Use the primitive drawing functions in lib/ivis_opengl/ (or lib/ivis_common/) to do this. That way you won't risk trying to draw while the graphics state is not suited for drawing. Also note that x and z denote the horizontal plane, while y gives the vertical plane for drawing, but this is not consistent throughout the codebase at higher levels.
Hmm, I would if I could find out what function does what I want. I found some line-drawing methods which seemed basic enough, but they are screen space based. Also when I tried to make my own drawing method using glVertex3f, nothing appeared, even though I took your note into consideration:
pie_Line(100, 200, 300, 500, WZCOL_GREEN); Showed a nice green line.
pie_Line3d(10, 0, 10, 200, 200, 300, WZCOL_WHITE); Showed nothing. (pie_Line3d() is exactly the same as lie_Line() except with glVertex3f and two z-coordinates)

It seems the best (only) idea is to convert world space coordinates to screen space at every screen update or something, it just seems impossible to draw directly from world space. I was looking if there were any functions for that, came across pie_RotateProject which seemed promising, but when I tried it out it didn't exactly do what I hoped.

Code: Select all

Vector3i worldposition;
Vector2i screenposition;
SDWORD cZ;
	
worldposition.x = psFormation_gpf->x;
worldposition.z = psFormation_gpf->y;
worldposition.y = -300;

cZ = pie_RotateProject(&worldposition, &screenposition);
It did give some results, such as
worldposition.x,.y,.z = 1104 10 6819
screenposition.x,.y = 677 191
but this was not anywhere near the tanks, and screenposition.y was dependent only on worldposition.y (as in, stayed the same most of the time even though the tanks were moving all over). So it's somewhat what I had in mind, but it doesn't seem to work as I expected. Can you tell me if there are functions that could do this conversion from world to screen, and if so where they are?
Phoenix946
Greenhorn
Posts: 9
Joined: 02 Jun 2009, 16:13

Re: Concerning ingame coordinates representation

Post by Phoenix946 »

It appears that pie_RotateProject is the right method after all, but I can't figure out how to set the matrix context correctly. I got it working for droids using calcScreenCoords() in display3d.c, called from displayCompObj() in component.c because the context there was already set, as said in the comment above displayCompObj(): /* Assumes matrix context is already set */. But I just can't seem to find out how to set the context so that pie_RotateProject would use my own object (the formation) in display3d.c.

I'm pretty sure it's something using methods as iV_MatrixBegin(), iV_TRANSLATE() and iV_MatrixRotate*(), but I'm missing the complete picture, and in whichever way I use these methods, I get the same result as what I described in my previous post. I've tried replicating the usage as it is in renderAnimComponent() in display3d.c but to no avail, and I've also tried using the same methods as used in component.c from where calcScreenCoords() is called (setMatrix() and pie_MatScale()) but that also gave the same results. So frankly I'm at a loss on how to set this matrix context correctly. Can you help me out?
Per
Warzone 2100 Team Member
Warzone 2100 Team Member
Posts: 3780
Joined: 03 Aug 2006, 19:39

Re: Concerning ingame coordinates representation

Post by Per »

I've been fighting the same functions without getting a lot wiser, myself :|
Phoenix946
Greenhorn
Posts: 9
Joined: 02 Jun 2009, 16:13

Re: Concerning ingame coordinates representation

Post by Phoenix946 »

Hmm, I don't like hearing that from a developer O_O But thanks for telling, if I give up at least I'll know it's not just something stupid that I overlooked.

Instead of trying to make a seperate context for it, I tried integrating it in the already existing context today, which had some interesting results: I have what I think are the right screen coordinates now, but when I try to draw them (using iV_Line() ), they don't appear on the ground but in the air, and seem to switch in dimension when I rotate the camera. The forms I got from drawing from the droids are also drawn that way, while they were actually drawn properly on the ground before I tried this integration thing. And that without any extra iv_MatrixBegin() or iV_TRANSLATE() in between :| Any idea what might be going on there?