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.

Newcomers DBPro Corner / Camera help pleeez

Author
Message
Jp in da house
18
Years of Service
User Offline
Joined: 29th Apr 2006
Location:
Posted: 29th Apr 2006 12:46
Hi

im new to darkbasic and i understand some basic manouvers such as loading terrain and objects, and controlling them with the keypad. but i am having trouble with cameras. i have loaded an object and i want to make the camera follow the object around whilst the object moving. i tried adding cameras but i just end up with a green screen????

thnx jp
Twu Kai
19
Years of Service
User Offline
Joined: 2nd Oct 2005
Location:
Posted: 29th Apr 2006 15:46
You can use this:


the command is the "set camera to follow object position x(1),object position y(1),object position z(1),object angle y(1),500,150,30,0" command.

Join The Clan! http://www.torak.info

Desteya colatiata hethayas thorian helltastha lutanithan atelthuron ilonthes!
RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 29th Apr 2006 16:28 Edited at: 29th Apr 2006 17:46
First off its always nice to post what language you're using. Ill answer using DBP methods as I've never used DBC before.

Every single DBP app (unless specified otherwise) always has a camera to begin with right when a single 3D command is called (loading an object, creating one, etc.). This camera's number is 0.

Making cameras will only create cameras and positio them at 0,0,0, if you want a camera to follow an object around you have to program this yourself (or use the set camera to follow object command, but Ill explain using something else as you wont learn much from that.)

So, we've established that there is no need to create another camera as there is automatically one in the game labelled 0. First, lets setup a basic program with a camera looking at an object from above/behind:



Hopefully you understand what the code other than the camera commands is doing. The first camera command we call is the AUTOCAM OFF command. When AUTOCAM is ON, the camera will automatically be positioned/pointed at the next loaded object, which isnt what we want in most cases (we'd rather have complete control and position it ourselves.)

The next two commands are POSITION CAMERA and POINT CAMERA. These are fairly straight forward; the POSITION CAMERA command positions the camera at the x,y,z coordinates you provide, in this case 0,200,-300. The POINT CAMERA command points the camera to the 3D position you specify, in this case 0,0,0. Simple?

You may have also noticed that we dont specify a camera number. This isnt needed since the camera we're working with is the default camera, 0. If we were working with camera 1, 2, 3, etc. we'd have to use the camera's respective numbr however.

Ok, now lets add some movement to the cube and a basic cam positioning system. First the code, then the explanation;



Ok, first off run that and see what it's like. (You'll notice I added in a matrix there so you can see yourself moving around).

You'll also see that I removed the original camera positioning/pointing code. That is because it was redundant since we'll be overriding that code by positioning it elsewhere inside the loop.

Alright, first off we have the basic movement code (I hope you understand how this works). Then, we store the object's current X,Y,Z positions into variables X#,Y#,Z# respectively. The reason we do this is mainly because it's easier/faster to type X#,Y#,Z# instead of OBJECT POSITION X,Y,Z all the time, though there is a tiny amount of speed-up in terms of game speed as well.

Next, we position the camera at X#,Y#+20,Z#-60. If you think about what this is doing it translates into positioning the camera at the object's x position, the object's y position plus 20 (so a bit higher than the object), and the object's z position minus 60 (so a bit behind the object).

Finally, we point the camera to the object's current X,Y,Z position so we can see the object.

You'll probably have realized right when you ran the code that this isnt working too well, as the camera isnt turning with the object, instead it's just staying behind it in one direction. Im pretty sure this isnt what you wanted, so Ill explain how to fix this using 3 methods. First using limbs, Second using a "generic" method, and Third using maths.

Using Limbs

I guess I should start by explaining what limbs are. Limbs are basically objects (they're not but we can think of them as objects for the time being ) that take on the position/rotation of another object. Load this code into DBP to see what I mean (the two spheres are limbs of the box object).



You'll see that as you move around, the two spheres follow the cube perfectly, as if they were part of the object. And this is in essence what limbs really are, extensions of the object.

So, how do limbs work?

A limb requires three things; a parent object number (in the example above, the parent object was 1), a limb number (just like an object number, a limb number identifies the limb, and finally a mesh number (a mesh is used to tell the limb what to look like.

Before we make the limb, we need the parent object. We accomplished this by creating the box, as it was our parent object in the example above.

We also need the mesh. To make a mesh, you use - you guessed it - the MAKE MESH commands. However it's not just MAKE MESH, its MAKE MESH FROM OBJECT. What this command does is creates a mesh (as mentioned before a mesh is the data that tells a limb what to look like) from the object specified; in the example above we created a mesh from the sphere object. But guess what, mesh's require ID numbers as well! So we numbered it 1. So, all together, MAKE MESH FROM OBJECT 1,2 creates a mesh numbered 1, from object 2. Its important not to confuse the end numbers as a lot of people tend to switch them accidentaly and make the mesh's number 2, from object 1.

Now that we have the parent object and mesh, we're able to add the limb. As you can see we use the ADD LIMB command. This command (as mentioned before) requires the parent object number, the limb number, and the mesh number, respectively. So as you can see in the above example we used ADD LIMB 1,1,1, which adds a limb to object 1, with an ID number of 1, using mesh 1.

The next limb command used is the OFFSET LIMB command. This is fairly straight forward, think of this like the limb's version of POSITION OBJECT. Basically the first value specified the parent object, the second specifies the limb number, the third specifies how far left or right of the object the limb should be positioned, the fourth specifies how far high or low from the object the limb should be positioned, and the fifth specifies how close or far from the object it should be positioned.

Finally, we delete the MESH and the object used to create the mesh, since we dont need them anymore.

Alright, now that you *hopefully* understand limbs, lets move onto using them for camera positioning. First lets think of what we want to do;

We want to position the camera behind the object at all times, above the object a tiny bit, and point the camera to the object's position.

Can you see the connection?

We can add a limb to the main object, position the limb behind the object and a bit above the object, and then use the limb's X,Y,Z position to position our camera!

I dont think I need to explain this now that you understand limbs and the basic cam commands, so here's the code;



Using the "Generic" Method

Thsi method is a lot easier than using limbs, but I just thought Id show you the limbs in the hopes that you might see another use for limbs. Ok, here's the code, then Ill explain:



Ok this is extremely simple. First off we handle the object movement, then we store the object's positions and angles on the X,Y, and Z axis. With me so far?

Then we position the camera at the object's position, rotate the camera to the object's angles, move the camera backwards 10 units, and upwards 20.

What have we accomplished? Well by rotating the camera to the object's angles, the camera will always face the same direction as the object. By positioning the camera at the object's position and then moving it back/up, it will always stay behind the object as well, since the camera is facing the same direction as the object.

Ok, final method;

Using Mathematics

Alright, this last method, I dont claim to be a pro with at all. To tell you the truth, I dont even know how/why it works, but Im sure one of the math gurus around here can help you out. However, here's some basic code using maths for you to play with;

<EDIT>

Well... I cant seem to get the maths working right now , perhaps someone else will show you the method using maths but either way the above two methods are still widely accepted.

Hope it's been of some help,
- RUC'

Pincho Paxton
21
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 29th Apr 2006 17:08
It's DBPro if he's getting green screens, and multiple cameras.

Jp in da house
18
Years of Service
User Offline
Joined: 29th Apr 2006
Location:
Posted: 29th Apr 2006 23:23
omg thanks for all your help guys it has really given me some confidence with the language

Jack
Jp in da house
18
Years of Service
User Offline
Joined: 29th Apr 2006
Location:
Posted: 30th Apr 2006 10:10
Hi one more thing could you please tell me how to setup a camera for a first person shooter???

thanks for all your help
RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 30th Apr 2006 16:59 Edited at: 30th Apr 2006 17:01
First person shooters are a lot easier, as usually the only position that differs from the "characters" position is on the y axis.

For starters, the basic code;



Ok, other than cam handling we've got the basic engine setup. I'm assuming you're looking for the ability to rotate the camera with the mouse,Ill explain that method once we get the basics down.

First off lets get the positioning, then we'll worry about rotating. Positioning is as simple as constantly positioning the camera at the sphere's coordinates. So;



If you run that you'll see that you can move around and the camera moves with the sphere. However, it isnt turning with it. Though this is extremely simple to solve, using part of the cam theories from my other post, we constantly rotate the camera to the object's rotations. Like so;



Now you have a basic first person view style. Just a note, instead of storing the object's angles, if you're not altering the camera's position to be different from the object's position, you could also do this;



Which basically makes sure the camera is always pointed at the object's new coordinates. It does the same as the other method, with relatively no speed difference. The only downside is if you're altering the cam's position to be higher than the object's position, this wont work.

Alright, now that we have that done lets get into using the mouse. The mouse has a lot of handy commands for it just like the camera.

The screen coordinates on any computer monitor are fairly different from the 3D world coordinates. For one thing, there are only two dimensions; X and Y. X being left and right across the screen, and Y being up or down. The other main difference is the coordinates begin in the top left of the screen. So, 0,0 is the top left coordinate, and if you had a screen 800 pixels across and 600 pixels down, the bottom right cordinate would be 800,600.

The two main commands we need are the mouse movement commands;MOUSEMOVEX() and MOUSEMOVEY(). These commands return the speed the user moves the mouse at on the x and y axis of the screen. Once we have this speed, we can determine how to rotate the camera accordingly.

So, first, we store the mouse speeds on the x and y axis;



You're probably wondering why X# is set to the mouse movement on the y axis, and Y# is set to the mouse movement on the X axis. Well, if you think about it, x n the 3D world is up and down, and X in the 2D world is left and right. Whereas it's opposite for Y (being left and right in 3D but up and down in 2D). This is why we switch them, hopefully that makes sense.

Next, we need to slow down the movement, as usually people move their mouse very fast. This next section of code can be thought of as the "look sensitivity", changing the slow down value will either increase or decrease the speed of rotation. We'll use 0.1 for now;



The last step before we can rotate the object/camera is to add their angles to the X# and Y# values.



The wrapvalue command basically makes sure any value is within 1 and 360. Since there are 360 degrees in a circle, this is necassary. Basically, eventually if we just kept adding the object's angles to the mousevalues, we'd either get something above 360 or something below 1, which technically is a possible angle (as -90 degrees = +270 degreesm -180 degrees = +180 degrees, etc), but it can affect other things later in the program if the angle isnt a proper positive value between 1 and 360.

Anyways, now we have our X and Y angles that we need to rotate the object and camera by. Now its as simple as plugging them in;



Which, completed, gives us the below code;



You'll also notice I changed the left/right arrow keys from turning the object to the usual straffing, since the mouse is now used for rotation.

Just some extra things I thought I'd add;

- As of now, the object can "fly". This is because we're altering the X angle of the object. To stop this flying, change the line ROTATE OBJECT 1,X#,Y#,0 to ROTATE OBJECT 1,0,Y#,0.

- This system is using the arrow keys. If you're looking into using the standard FPS A,S,D,W keys, you'll probably want to look into using the KEYSTATE() command.

Here's the final code, commented, and fixed for "FPS"ness. If you need help with how KEYSTATEs work, look them up in the index or do some searches on the forum (Please, dont post a question about something thats already been answered a billion times. Theres been a lot of keystate questions in the past, Im sure with some searching you'll find your answer.)



(I changed the X# and Y# values to be relative to the Camera's Angle instead of the Object's Angle (In the "Calculate New Angles" section.) Change it back to Object and you'll see why)

Goodluck,
- RUC'

<EDIT>

Oh, and if you're looking for a tutorial on making FPSs, do a search in The Newcommer's Corner for "FPS". There's a post entitled "The Newcommer's Guide to Creating FPSs". Its not complete, and the site is down, but there's a download for the tutorial there. It should help you get started a bit.

Jp in da house
18
Years of Service
User Offline
Joined: 29th Apr 2006
Location:
Posted: 30th Apr 2006 20:31
Thanks again for all your help

jack
Xender
18
Years of Service
User Offline
Joined: 28th Apr 2006
Location:
Posted: 2nd May 2006 03:51
I got a questions really quick, i kno it is possible to make the guy strafe left and right (liek just move left and right not rotate the camera by using A and D) so how would you do that having it be done by an object. I have code that moves the camera, and then i just had the cube be equal to that spot (is that just as good?) and could strafe left and right, can you do that with an object as well?

Login to post a reply

Server time is: 2024-11-26 21:28:44
Your offset time is: 2024-11-26 21:28:44