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.

Dark GDK / Need help understanding 3D, Pls... (NOTE: another *long winded* post by JTK)

Author
Message
JTK
14
Years of Service
User Offline
Joined: 10th Feb 2010
Location:
Posted: 16th Apr 2010 07:27
First off, let me just state that C++ is *not* a problem for me, so if you feel you can contribute, feel free to do so - I\'m not a beginner in that regard...

Next, let me explain the scenario:

I\'m planning on submitting an entry into the competition thread that\'s due by April 30th (http://forum.thegamecreators.com/?m=forum_view&t=167521&b=22). My approach is to use 3D (mainly to help me understand 3D) as a 2D platform for the side-scroller effect.

I plan on using characters from the Dark-Matter media installed as a part of GDK (i believe they\'re a part of GDK anyways - I bought the DarkGame Studio package so I\'m not sure if it includes the same media or not). In fact, all of my characters in the game come from Dark-Matter (except for the main character - as I\'ve yet to find a blob of ooze that can transform into a shield, boot, glove or spring [or any animated blob of ooze for that matter] online, I\'ve got to substitute concept drawings for the main-character).

But, I digress...

Anywho... I want to place all characters on one z-plane (to aide in collisions - I can handle collisions btw), back drop props on other z-planes and the camera on a z-plane parallel to the action. The camera will focus on a point and only move to another *if* the main character breaches the radius of a pre-determined circle of view (note this will be an inner-circle of view to allow the character to move freely within the screen-space). I do understand the math involved in determining when the camera should move (simple sphere/circle intersection tests (or circle/circle tests in 2D parlance) would suffice - i think) and so that\'s not the problem...

Instead, my problem is this...

How do I place objects, all on the same z-plane, on the screen? Note, this is a scroller so I expect the camera to move from time to time. However, my thinking is (and maybe this is where I\'m wrong) since everything should be parallel on z-planes, all equations should degrade to 2D calculations. Am I wrong about this?

In short, I am asking this...

If all *objects* are to be located on the same Z-plane (or further from the camera), how do I place them on the screen? That is, assuming for instance the z-plane is -100, with a screen dimension of 1024 x 768 (60-degree field of view); where do I place an object x so that the camera can see it plus the proverbial radius of the far end of the view cone? Additionally, where do I place the camera? More importantly, how do I determine these locations?

The Old Proverb: \"Give a man a meal, and you\'ve fed him for a day; teach the man to fish, and you feed him for a life-time\" comes to play here. I\'m not looking for the meal, I\'m looking to learn how to fish!

Please, help me understand 3D, I\'ve been trying for going on 8-years now and everyone says \"imagine the camera is fixed, the world revolves around the camera\". Ok! I get it, everything else moves, not the camera; I understand that; but I don\'t understand *why* that\'s so - nor do I understand *how* to make everything else move...

As everyone here, including myslelf, would prefer to see code; here\'s some code that don\'t work for me... Why? What am I missing?




Why does this not work since the calls to mObject[x].XXXXXX essentially calls the GDK equivalents? I\'ve narrowed it down to my understanding of 3D... Remove the x,y,z variables and SetPosition() and it works fine!

Again, I plead, please help me!

JTK
haliop
User Banned
Posted: 16th Apr 2010 10:03
i guess the constructor of CLitterBug_Babe dupilcate the object
but where do you load the object?

from this side of the screen , it should work as long as you have an object to work on...

and maybe the last line inside the loop should be the first if its the object model choser... if not .. nvm..
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 16th Apr 2010 10:13 Edited at: 16th Apr 2010 10:17
you want the camera to be over him, so use the x and z as x and y, and use the y as the depth, since y is up unless you're reversing the whole scene, tho i see it more simple to use the y axis for depth, isnt it simple as any 3d game, but all objects are on the same y axis and the camera is over the player? or maybe i didnt get the point?

also: imagine the camera fixed and everything moves, well that's in 2d, in 3d it's easier to move the camera, and make everything fixed

Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 16th Apr 2010 12:47 Edited at: 16th Apr 2010 15:20
First, in 3D view, the X axis points to the right, the Y axis points upwards and the Z axis points into the screen, away from you. When you position the camera to 0,0,100, you are actually moving it forward, not backward. When you next point the camera to 512,384,0, it will look towards you but at a slant angle, since it is looking at a point to the right and above its current position. That may produce a strange visual effect. What you want is to look at your objects at a right angle.

As I see, you are positioning your objects randomly between X= 0 to 1024 and Y= 0 to 768 range. That means the camera should sit in the middle of that X/Y field, and should be moved on the Z axis backwards (behind the viewer's back, so to say), therefore:



I do not see any code where you create or load the objects, so check if they are loaded at all. Then, keep in mind that whenever you create or load an object, the camera is automatically moved and rotated so that it looks at the new object. That means that in your code, if you create the objects at 0,0,0 then the camera will auto-point at 0,0,0 and afterwards you position the objects away, so that may be reason why the camera does not see them any more. Position and point the camera after you have loaded everything and also you can use the dbAutoCamOff() command to switch off this auto-pointing behaviour of the camera.

Depending on the size of your objects, the -100 distance of the camera may be just good, or it may be too small or too large. You could try to calculate based on the field of view what the ideal distance should be, but I think you can just experiment it out, by starting the program with different camera distance values, or include code which moves the camera forward or backward in Z coordinates as you press up/down keys and see how much of your objects you see on the screen.

If this is going to be a 2D-looking side-scroller, then you may attempt to remove the 3D perspective effect from the view, so that it will look "flat". There was an article in an old newsletter which said that if you set the range and field of view of the camera to very large values, it is possible to eliminate perspective. I don't remember the exact values, I think the Z range was around 30 thousand and the field of view was between 180-360. I'll try to find that article and post back here if I found it.

Last, keep in mind that the screen resolution in 3D view has absolutely nothing to do with the coordinates of your objects. In 3D you work with world coordinates, not pixels. The world coordinates can be meters, miles, or any arbitrary unit you are comfortable with. You can set the coordinates of your objects and you can also move your camera wherever you wish. If you change the screen resolution, it will not change the view (like the amount of sprites you see would change in 2D if the screen is set to different pixel values), you will just see exactly the same view in lower or higher resolution. (Note: if you change the screen size during the game though, like resizing the window, then the view will be distorted but you will still see the same amount of objects on the screen.)

You were correct about one thing: if all objects are on the same Z plane, coordinate calculations are basically reduced to 2D.

I hope that helps. Ask away if you have further questions.
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 16th Apr 2010 13:45 Edited at: 16th Apr 2010 15:18
P.S. two more comments:

Quote: "The camera will focus on a point and only move to another *if* the main character breaches the radius of a pre-determined circle of view"


If it is a side-scroller where the camera should keep the player always in the centre, the simplest way is to set the X coordinate of the camera in every loop to the same value as the X coordinate of the main character. But maybe you want to achieve a different visual effect with this method? Naturally, how you want the camera behave is up to you and the needs of the game.


Quote: "'ve been trying for going on 8-years now and everyone says \"imagine the camera is fixed, the world revolves around the camera\""


You can boldly forget this when using Dark GDK. That advice may be true if you are writing your own 3D rendering engine. I remember writing 3D functions in the "old days" under DOS and then the books really advised the programmer to "invert the camera matrix" and rotate-reposition all the world objects, because the following screen rendering calculations were simpler that way. Dark GDK takes that off your shoulders. In 3D you have the camera commands to enable you to look at the scene from any angle and position you want, so do not start to reposition all your objects when all you want to move is the camera.
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 16th Apr 2010 15:16
And the next P.S. I found that newsletter article:

http://www.thegamecreators.com/pages/newsletters/newsletter_issue_67.html

Look for platform game serial part V. Of course it is not compulsory to try to remove perspective, it entirely depends on which way the game will look better.
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 16th Apr 2010 16:34
OK this is the last P.S. for today. I made an experiment. If I create some cubes and position them at the coordinate range you use (1024, 768), I have to set the camera Z distance to -620 to see all of them. The -100 distance is not enough. That might be another reason why you do not see the objects.

Another tip: at close range, it is easy to control how much of the scene is visible in the view by changing the camera Z distance. If you do opt for removing the perspective by setting a very large camera range, then the visible amount of scene is easier to control by changing the field of view.
JTK
14
Years of Service
User Offline
Joined: 10th Feb 2010
Location:
Posted: 16th Apr 2010 18:44 Edited at: 16th Apr 2010 18:46
Thanks everyone. I appreciate your responses.

@Mireben - Would you attach that sample code please?

Thanks again to all,

JTK

P.S. BTW - all of my media does load just fine. If I remove the five lines of code dealing with x,y,z and SetPosition, I see all of the objects blobbed together in one place.
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 16th Apr 2010 19:23 Edited at: 16th Apr 2010 19:28


i think that's what you are after?

using GDK i find it MUCH easier to create a 3d game, than a 2d game, since it keeps you off maths

JTK
14
Years of Service
User Offline
Joined: 10th Feb 2010
Location:
Posted: 16th Apr 2010 20:15
@Hassan: That works great... However, if I replace the first dbMakeCubeObject(1,30) with dbLoadObject("Media\\H-Babe-Move.x",1) I would expect to see the Babe moving animation as I scroll. I don't get that - in fact I don't see any "player" object (I do still get the 200 or so other cubes).

Help me get her up and moving and I think I can take it from there.

Again, thanks. That is what I was looking for.

JTK
JTK
14
Years of Service
User Offline
Joined: 10th Feb 2010
Location:
Posted: 16th Apr 2010 20:44
Whoop! There she is! I found her... lol.

I added two more keys (ctrl/shift) that moves the camera closer / farther. I then walked the player outside of the group and kept zooming until I saw a flicker... turns out, that by comparison to the cubes, she's teensy-weenie... guess I better scale her up, eh...

Thanks for all of your help everyone. With this block given by Hassan and comments by all of you, I think I can take it from here!

JTK
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 16th Apr 2010 20:51 Edited at: 16th Apr 2010 20:54
well, probably too huge model, or too small models, try removing dbAutoCamOff() and see the result, if you cant see any objects, only your model, then its too small, but if you can see everything small, but your model is big, then its too large, then you can scale it to fit the scene

i will explain the code since it's what you're looking for:

first, we position the camera at 0, 300, 0. and then point it to 0, 0, 0, so as i said, we will deal with it like that: y is the depth ( positive depth = farther from scene, negative = closer, 0 = at scene ) x and z are x and y, but z grows upwards, not downwards, like y in 2d, ( note that this is mostly used in sidescrollers, not in all kinds of games )

so there we have our camera at 0, 0 and depth is +300, now we position objects randomly between -500 and 500, and depth(y) is 0.

and in the loop we check movement, like this:

so:
right button = x+
left = x-
up = z+
down = z-

you can use the y for the camera range, larger value is farther camera, thats why we keep out camera position at obj x, 300, obj z : obj x and z ( which we called x and y ) so the camera is always on the top of the player, and 300 is the range.

note that all object's depth ( y ) is 0, so if the camera y is negative, you wont see anything, also 0, at 1 you might see the ground, but that's really small range between the ground and camera, range will depend on your world's size.

also note that you can adjust the visible range using dbSetCameraRange, for example, you can set the lower range to 200, then you wont see any object closer than 200 units to you ( that's useful if you want to make layers and stuff )

Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 16th Apr 2010 20:56
oh you beat me to post =p, glad you got it working, good luck

JTK
14
Years of Service
User Offline
Joined: 10th Feb 2010
Location:
Posted: 17th Apr 2010 03:12
You guys don't know how happy you've made me...

Here's the modified code (from above) that does exactly what I want and more importantly, I *finally* understand things (until my next snag).

Sorry Hassan, but I made the Z-Plane my distance factor (not the Y-Plane as you suggested) but it all finally makes sense to me now. And Thanks to GDK (TGC) for making it more like I would envision it... The player moves around the world, the camera follows the player - not (and I still don't understand this part) everything moves around the camera...



Note: The CKeyboard/CMouse class source files are published here

And for your viewing pleasure, I am even attaching a screen-shot of what I had envisioned in the first place...

So now, with all of that stuff falling into place, maybe I will be able to complete a playable version by the deadline...

Again, many thanks to all who have helped!

JTK

Attachments

Login to view attachments
Poof Master
14
Years of Service
User Offline
Joined: 14th Jan 2010
Location:
Posted: 17th Apr 2010 03:16
Are there any advantages to making the Z axis point up and the Y axis be the depth? I haven't really tried anything 3d yet but I am planning on doing the same thing JTK is going to do with the 3d side scroller. Since I've been using 2d I am used to it being Y up and X left and right. Would there be benefits (when I actually start coding) to change that? Or is it just preference?
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 17th Apr 2010 08:01 Edited at: 17th Apr 2010 08:21
well not really, but in directX, Y is the up vector, so for me it makes sense that the camera should be up, and looking at objects below it in the sidescroller games

EDIT: when i saw your screenshot, i realized that i mixed up between sidescrollers and topdown shooters so what you are doing is actually correct

Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 17th Apr 2010 10:39 Edited at: 17th Apr 2010 10:41
JTK: I'm glad that you got it working!

Even though you don't need it now, just for clarity I've made two sample programs to illustrate this perspective thing that I was talking about. The first program displays some cubes at a close range to the camera. The left/right keys move the camera sideways, the up/down arrows move the camera closer or farther away from the cubes. The current distance (Z) of the camera is printed in the corner of the screen. You can see that the cubes have a 3D perspective look, but the farther away they are, the less this perspective is. The camera distance influences how many cubes are visible within the screen area.



In the second example, the cubes are displayed very far away from the camera. The camera range is adjusted accordingly and the field of view is set to a very small angle. This is needed to "zoom in" on the objects, otherwise they wouldn't be visible from this large distance. The left/right keys move the camera sideways, but the up/down keys will, this time, change the field of view of the camera, because changing the distance would have no visible effect at all. The current field of view angle is displayed in the corner.

The difference is that, even if you zoom in close onto a cube, the perspective effect is not visible. Only the front side of the cubes can be seen, so they look like flat rectangles. This is the trick that the author of the newsletter article applied to make his program look like 2D.

Login to post a reply

Server time is: 2024-11-20 01:21:43
Your offset time is: 2024-11-20 01:21:43