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.

DarkBASIC Professional Discussion / Locate 3d coordinates from a 2d reference

Author
Message
A r e n a s
15
Years of Service
User Offline
Joined: 9th Jun 2008
Location:
Posted: 2nd Jun 2011 12:39
Hi everyone,

I'm around an rts game and I've been searching the forums for a bit and come to the conclusion that there is no previous thread which has a satisfactory answer to this problem. I was wondering how you would go about having the gamer click on the screen (this mouse click will obviously collide with the rts terrain matrix) and thereby gather the coordinates for the clicked location in 3d virtual space.

I've been looking alot into using trigonomety to locate a clicked location, giving a viewing angle, x and y mouce click location, and a flat plain of y=0 for intersection. I've realised that there is an issue here as the viewing angle of the camera will play havoc with any x and y coordinates I gather this way. I also have the problem that the terrain may not be and a height where y=0, causing further problems.

Is there an easy way to gather the clicked mouse coordinates?

[i]A[i]

Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 2nd Jun 2011 12:43
You could have a look at the 'Pick Screen' command and other pick commands.

A clever person solves a problem, a wise person avoids it - Albert Einstein
A r e n a s
15
Years of Service
User Offline
Joined: 9th Jun 2008
Location:
Posted: 2nd Jun 2011 12:51
That still wouldn't provide an adequate solution because I then get given the task of locating the distance to the click, and in order to do that it would obviously be easiest to gether the coordinates and use Pythagoras' theorem, bringing us back to the original problem of getting the coordinates

A

Neuro Fuzzy
16
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 2nd Jun 2011 13:00
It's easier just to take the whole terrain and plug it into Sparky's collision, then do a raycast from the camera's position to a pick screen() coordinate.

The second easiest solution would be to have a ray cast out from the camera intersect with a plane on the x/z axes. Basically, you have the camera's position (Cx, Cy, Cz) and the pick screen coordinate (Px, Py, Pz), so for a line formed by those two coordinates, which intersects a plane on the XZ axis, you have a system of equations:
Lx=(Cx-Px)*n+Px
Ly=0=(Cy-Py)*n+Py
Lz=(Cz-Pz)*n+Pz
for some arbitrary n. Just solve the system of equations for Lx, Lz, and you have the point of intersection.

That's not accurate when you want to click on mountains though.


Tell me if there's a broken link to images in a thread I post, and I'll fix 'em.
Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 2nd Jun 2011 14:24
Quote: "That still wouldn't provide an adequate solution because I then get given the task of locating the distance to the click"

Isn't that what the "GET PICK DISTANCE" command is for?

A clever person solves a problem, a wise person avoids it - Albert Einstein
Sven B
19
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 2nd Jun 2011 16:35
I think so too.

By using the pick commands, you'll probably have a better performance as well since DBP can use the internal camera data (meaning the view and world matrix). You can get the 3D data simply by using

get pick vector x()
get pick vector y()
get pick vector z()
get pick distance()

Cheers!
Sven

DVader
20
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 2nd Jun 2011 17:31
I posted a bit of code in this thread months ago.

http://forum.thegamecreators.com/?m=forum_view&t=178519&b=1

I think it is what you are after.

http://s6.bitefight.org/c.php?uid=103081
A r e n a s
15
Years of Service
User Offline
Joined: 9th Jun 2008
Location:
Posted: 3rd Jun 2011 00:38 Edited at: 3rd Jun 2011 00:54
Thanks guys, DVader has the exact code im looking for, those four key lines of code really are a god save!!!



Thanks for all your advise people! I'm not too brilliant with ray casting yet, so that's something I've avoided this time, but will have to face soon...

NB, its not working for me entirely. Is there a way to help this work with terrain? - Thanks again

A

DVader
20
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 3rd Jun 2011 03:48 Edited at: 3rd Jun 2011 03:58
As long as you place the initial plane down at the right point you should just have to add the y height of the terrain to get it to work. I worked on an RTS using it with terrains no problem. Just make sure the plain matches the terrain x and z exactly. Easy enough to work out the height from there. At least some of my unfinished projects have helped someone!

http://s6.bitefight.org/c.php?uid=103081
A r e n a s
15
Years of Service
User Offline
Joined: 9th Jun 2008
Location:
Posted: 3rd Jun 2011 12:26
Thanks, Ive managed to get this all working on a 2D plain now, I'm just going to order my variables a bit using a few type structures, and add some animation to the building of these units I'm adding.

My only question now is whether clicking on a high part of the terrain will mean that the location gathered by this algorithm will be inaccurate, as the hight of the terrain will not be taken into account. Do you see this as a problem or would it be possible to work around the issue?

Thanks guys

A

Tom0001
18
Years of Service
User Offline
Joined: 30th Dec 2005
Location:
Posted: 3rd Jun 2011 19:26
I assume you say the only reason that it is inaccurate because of the y/height value, yes? If so, you could just take the original algorithm and modify it to include the height of that terrain at the co-ordinates by using GET TERRAIN HEIGHT() to make it more accurate.
A r e n a s
15
Years of Service
User Offline
Joined: 9th Jun 2008
Location:
Posted: 3rd Jun 2011 19:42
That's not what I mean. Say your looking at the terrain from a hight of 100 and angle of 45. If the terrain your selecting is 100 higher than the flat plain of the main map, then the area you select will be displaced by the distance of the camera intercept of the plain and the location directly below the terrain which you've clicked and were intending to place the object.

A

Sven B
19
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 3rd Jun 2011 21:44 Edited at: 3rd Jun 2011 21:46
I suddenly got the urge to summarize it all up for a bit, so here I go:

Finding 3D point from screen coordinates at a certain distance
To find a 3D point under a screen coordinate at a certain distance Distance# you can use the command

pick screen X, Y, Distance#

The calculated vector is relative to the camera's position, and can be retrieved using
get pick vector x/y/z()

So to get the world coordinates (not relative to the camera) you just add the position of the camera:


Finding 3D point from screen coordinates at a certain height
Basically, the method is the same as the previous, but you rescale the vector so that the y-component of the pick vector is fixed. Let's say that we want to find the point at height h#.



Finding 3D point from screen coordinates on an object
Instead of using pick screen, you just use

pick object(X, Y, StartObj, EndObj)
This command will check whether or not (X, Y) is over an object starting from StartObj and ending with EndObj. If an object is found, the command will return that object number, and will store the 3D intersection data in the pick vector commands.

Example:


There are unlimited possibilities...

Sven B

KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 3rd Jun 2011 22:29 Edited at: 3rd Jun 2011 22:34
I'm using BlitzTerrain, but the concept is the same.

Set up your terrain with Sparky's collision and put it in group 1.



Nothing else seems like it would be as accurate.

A r e n a s
15
Years of Service
User Offline
Joined: 9th Jun 2008
Location:
Posted: 4th Jun 2011 00:02
Thanks guys for all the help, special thanks to Sven B for the extreme breakdown. I've implemented all this and I'm continuing the expansion of my engine now

I'm leaving the terrain in a plain for now but when I come to add varying heights and building on these hight levels, I will definately come back to this thread and the above posts to clarify and debug.

Thanks again for everyone's help, couldn't have proceeded with this project without you all

A

Van B
Moderator
21
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 5th Jun 2011 03:19
As long as your using a 3D object for the terrain, you could PICK that object, and get the exact location that you clicked.

DBPro is well set up for making an RTS, I'm surprised there still aren't that many.

Health, Ammo, and bacon and eggs!
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 5th Jun 2011 10:58
Quote: "DBPro is well set up for making an RTS, I'm surprised there still aren't that many."

I started one long ago, there's just so much to do in an RTS the work is more involved than I originally thought.

DVader
20
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 6th Jun 2011 20:27 Edited at: 7th Jun 2011 00:59
Same here I started one last year. A Rome based city building game. I posted a screen shot of it once I think. It was intended to be an appup project but speed was an issue, especially when things started to get busy on a netbook. It was a collaboration with a friend and he lost interest in the project as well. So at the min it is another unfinished RTS I'll see if I can post a video of it up to show what was started, just for curiosity's sake.

Edit - Here it is. An old version as I can't seem to find the latest code or exe on this comp.



The jittery zoom is due to my faulty mouse wheel

http://s6.bitefight.org/c.php?uid=103081
A r e n a s
15
Years of Service
User Offline
Joined: 9th Jun 2008
Location:
Posted: 7th Jun 2011 17:13 Edited at: 8th Jun 2011 00:27
Wow! That looks great!!

So far the game I'm working on is going great, we've focused on buildings mainly and now we're just starting on expanding to begin work on units (im terrified of waypoints, it'll be a nightmare) and we're also beginning on advancing the terrain from the current flat default.

I'll get a video and see if I can embed it into this post.

EDIT



NB; this is in the very early stages, and with fraps recording this, the fps was brought down from 500+ to 60...

A

DVader
20
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 9th Jun 2011 15:15
Looking good so far there Arenas. The detail looks very good, just be careful you can place lots and lots of them down with no slowdown problems. Unless you plan to use LOD of course.

http://s6.bitefight.org/c.php?uid=103081
A r e n a s
15
Years of Service
User Offline
Joined: 9th Jun 2008
Location:
Posted: 10th Jun 2011 23:15
I think I may need to rewrite some of the algorithms because with over 300 in one map with 10 buildings being built with animations, the fps has slowed down by 10 or so frames from 500 fps. I'd rather have a minimum drop so I can work with as many features as possible. I am also aware the units will take up much more memory than anything else, and when I have more than one type of object loaded into memory at game start, I can imagine the drop may raise from 10 to 100 or so... I'd love to chat algorithms for speed regarding placement and builds.

Like would it be better to delete objects as they fall of screen and clone them as they appear back on, or if there were a change of 0 object to 1000 in a scroll, would that cause a sever drop in fps? And if so would it be best to leave all objects on the map?

Also, I have never looked into memory blocks much, but would they be good for storing the large arrays or all the pre-object possibilities (loaded at startup and then cloned when built)?

Anything else people may suggest regarding this game would be greatly appreciated.

I'm dreading starting work on waypoints... I've done it with a 2d array and a square grid system, but I'll find it trickier when it becomes an octagon grid. Not to mention I'm worried about the amount of issues related to processing power required to generate paths for say, 500 units at once...

A

Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 11th Jun 2011 00:12
How about a single dimension array that simply stores the location of each waypoint?

Quote: "generate paths for say, 500 units at once..."

You shouldn't need to. If you select 50 units at once and move them somewhere, your path finding could only generate a single path and then you have each unit follow behind another in a line. With that path stored, you could easily search for a new path just 1 square in front of each unit's position. That would be a quick search and would help when a lot of units are moving at once and can't all fit in a straight even line, or if they get bunched together and the mass bleeds out into surrounding tiles. Just check for new paths when a unit (or group of units) either changes target or the dynamic map updates and changes something on the current path.

Quote: "Like would it be better to delete objects as they fall of screen and clone them as they appear back on"

I think you should clone the objects from the start anyway. Deleting and then re-cloning I think may give more hiccups, but I couldn't say for sure.

A r e n a s
15
Years of Service
User Offline
Joined: 9th Jun 2008
Location:
Posted: 11th Jun 2011 01:35
Quote: "How about a single dimension array that simply stores the location of each waypoint?"


How would you go about working that one through? The 2d array earplugs system is pretty simple and with a screen of 10 by 10 tiles there is no change in fps. Obviously I'm looking at a screen of about 100by100 tiles roughly, but I would say that the 2d system of checking all points adjacent until finding a valid end would be best. I am aware that the time taken would have an exponential rate of increase, and if anyone who goes to university could tell me calculations for time taken as I believe uk universities teach that. I'm only in highschool but I'd love to hear what people have to say on that matter. Calculating times of algorithms would be something very useful obviously.

A

Login to post a reply

Server time is: 2024-05-19 19:28:25
Your offset time is: 2024-05-19 19:28:25