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 / Shooting problem

Author
Message
Data
19
Years of Service
User Offline
Joined: 26th Nov 2004
Location: Winnipeg,Canada
Posted: 30th Jan 2005 06:21
Hi,
Im still new at this and I was wondering if someone could show me
how to make a gun and make it possible to shoot. Could someone
please help?

please.......
Foxy
19
Years of Service
User Offline
Joined: 4th Jan 2005
Location: The Dale, South Australia
Posted: 30th Jan 2005 16:37
Well, I'll have a shot.

First things first, you need a gun model. There is a very simple gun model attatched to this post in a zip file. The image file in the zip file with it is there to keep the gun a constant grey colour, sorry but I didn't texture it.

So you have your gun model, now it needs to be loaded into your program. This is the code to do such a thing:



That will load the object and it will further be referred to as object 1.

As with any program you'll need some initial setup lines as below:



The first line will turn manual screen refreshing on, the second line will cap the frames per second rate to 50 and the third line will hide the mouse cursor. The do...loop commands are the heart of the program, these stop the system from running through all of the commands once and then end the program. All of the things you want to happen in-game should between these lines. Finally, the sync command simply updates the screen with anything that should be displayed.

Now that you have the main structure set up you need to add some commands that do something. This is where the "load object" command comes in. Because this is a command that is part of the before game setup it should go before do...loop statements, so you should have:



If you run the program now you'll notice that you can't see anything. This is because the gun is loaded in the same spot as the camera, by default, the camera does not render the inside of objects. What needs to be done now is to get it so that the gun is in the position so that it looks like the virtual player is holding it. To do this we need the "position object" command, the syntax for this is:



The obj# represents the object number that you want to move and the x, y and z parameters stand for the co-ordinates in 3d space that you want to move your object to. Once again this is a before-game setup command (in this circumstance at least) so it has to go before the do...loop statements. The code should look like this:



Now we have the gun in the right spot but, if you run the program, the gun is definately not pointing in the right direction. This will arise in some models depending on where their 0,0,0 co-ordinate is within the object and whether the object was rotated at all when being made, as in this model.

To eliminate this problem we need to use the "yrotate object" command, the syntax for this is:



Where obj# is, is the object number you wish to move and the degree is how many degrees you want to rotate the object. The y axis is the vertical one. With other objects getting the degree right can take some playing around but is easy enough if you start with 90 degree increments and work from there. This is pre-game code so, you know the drill. Your code should look like this:



Now that the gun is in the right spot, we want to make sure it doesn't go anywhere when we are playing so the "lock object on" command comes into play. The syntax is:



Once again the obj# represents the object number you wish to lock. What this command does is lock the object mentioned in the same spot according to the camera at all times. Pre-game, yes. Your code should look like this:



I'll continue on to shooting and moving around in another post in about a days time. If this code gives you any trouble just post and I'll fix it up. You may already know this much so far but I thought I'd put it all there in case anyone really new to DB could benefit from it.

Good Luck.

If I were you and you were me, then you'd be yourself 'cos you are me and I am you.

Attachments

Login to view attachments
Data
19
Years of Service
User Offline
Joined: 26th Nov 2004
Location: Winnipeg,Canada
Posted: 31st Jan 2005 10:41 Edited at: 31st Jan 2005 10:42
Thanks
I can't wait to see the shooting and moving around part.

Thanks again!
Foxy
19
Years of Service
User Offline
Joined: 4th Jan 2005
Location: The Dale, South Australia
Posted: 31st Jan 2005 15:27
Thats cool. Well here's the moving part:

With any fps, you'll need some walls, this is where the "make object box" command comes in handy. The syntax is:



The parameters are pretty obvious with this one. Once again, setting up the "room" is a pre-game procedure so this should go with the gun setup, allthough you should seperate the lots of code with a free line or two. Lets make a room which is 200 units wide, 200 deep, the walls 40 units high and being 10 units thick. to avoid having to put in extra lines of code to rotate the walls with the "yrotate object" command. This does get tricky when using lots of walls because you'll have to figure out all of the dimensions for the walls. When things become too tricky you're better off to make the level in a level editor.

Back on the game we need two walls that are 200 units wide, 40 units high and 10 units deep. On the other hand we need two walls that are 10 units wide, 40 units high and 200 units deep. Now it's time to put this into code. Your code should look similar to that below:



Now that the 3d walls are made they need to be put into the right spot in 3d space, once again we'll have to use the position object command. When you create a primative in DB using code the 0,0,0 co-ord will be in the absolute center of the object this is always the x-size of the object divided by two and same for the y and z axis. Allright, now that we have that sorted your code should look like that below:



Notice in the position object commands that both positive and negative numbers are used when positioning the walls, This was done so that 0,0,0 would be in the center of the "room". This is a basic diagram of what things look like in 3d space when looking at them from above:



After a while you'll get a bit bored with merely a screen with a gun and some walls on it and not being able to move around. To do this all we need is four lines of code, these four lines are all "if...then" commands. The syntax for these are:



What we are after is to check whether the arrowkeys are being pressed and relate an action to it, an example is:



This code checks to see if the up arrow key is being pressed by checking the upkey() variable. if this variable returns a 1 (which it does when the upkey is pressed) it will move the camera 1 unit in the direction it is facing. When we want to rotate the camera arount to turn we need to use the "turn camera left" and "turn camera right" commands the syntax for these are simple:



The value parameter is merely how many degrees you wish to turn the camera in the stated direction, of course these values aren't in brackets in the code. These camera movement commands are something that will happen during the game so they need to come in between the do...loop commands. Your code should look like this:



Now You can explore the basic room. Just something for you to try; fiddle around with the "color object" command and make the walls different colours. The syntax for it is:



Simply put in the number of the object you wish to colour in place of obj# and use the "RGB(r-value,g-value,b-value)" command. In the place of the r,g and b values place a number between 0 and 255.

If youre bored try and make a roof and floor by yourself.

Tune in tomorrow for the next episode of durdge39's magical adventure: shooting.

Cheers for now.

If I were you and you were me, then you'd be yourself 'cos you are me and I am you.
Foxy
19
Years of Service
User Offline
Joined: 4th Jan 2005
Location: The Dale, South Australia
Posted: 31st Jan 2005 17:18
I forgot to add: With the "move camera" and "turn camera left/right" commands that if you use dark basic pro you'll have to set up the syntax like this:



The cam# parameter is the number for the camera that you are moving, by default, this is 0. The increment is, of course, how far you want to move it.

I use DBC so if I'm wrong please correct me somebody (I remember reading this in another post).

If I were you and you were me, then you'd be yourself 'cos you are me and I am you.
Baggers
20
Years of Service
User Offline
Joined: 31st May 2004
Location: Yonder over dem dere hills
Posted: 1st Feb 2005 03:18
Wow Durdge, well done a very nicely started tutorial, keep it up, ill be sure to link Newbies to here if the need help in this area.
Data
19
Years of Service
User Offline
Joined: 26th Nov 2004
Location: Winnipeg,Canada
Posted: 1st Feb 2005 06:34
I cant wait for the next episode.
Thanks again!
Foxy
19
Years of Service
User Offline
Joined: 4th Jan 2005
Location: The Dale, South Australia
Posted: 1st Feb 2005 17:43
Ok, so we have our basic "room" and we can move around it. There is a gun on-screen but it doesn't shoot anything. This is where things start getting tricky, lets start with the easy stuff first though. We need something to shoot, so we can easily do this with a simple "make object sphere" command, the syntax for this is:



The obj# is, of course the number that this object will be represented by and the radius is the number of units from the center of the circle to its surface. This is again another pre-game exercise so your updated code should look like this:



You'll notice that I've added the "position object" command to position the bullet outside of the room so as to not get in the way. Also I've added some code to make a roof and floor.

To add the shooting code we are going to have to mount everything inside one big if...then statement and we are going to use some "flags" to check whether the bullet is in motion or not. To shoot we'll use the space bar. To make this happen the following code needs to be added in the main loop of the program (or between the do...loop statements):



This is an extended version of the "if...then" command that fits on one line. Any commands you wish to execute when the condition = true should go between the if and endif lines. Your code should look like:



Before we can continue we need to declare a flag that we can use in our shooting code. This code will be:



This code will declare a variable named "shoot", set it so the variable can only accept numbers (due to the # after the name of the variable) and set the value of the variable to 0.

We need to place this before the do...loop commands otherwise we will run into problems later because the machine will keep setting the shoot# variable to 0 and things will continue to fail.

To set the program to position the bullet outside when the shoot# flag = 0 we need to insert the following code after the if...then commands:



The updated code should look like this:



The following code will position the bullet where the player (or camera) is, set it so the bullet is pointing in the same direction as the camera and set the shoot# variable to 1:



Notice that I have used the "camera position x()" etc command in the position object line, these commands return the x,y and z position of the camera. Your code should look like this:



That code will put the bullet in the right spot but we need some code to make it move. This is that code:



This code will test if the shoot# variable = 1, and if so, it will move the specified object in this case, the bullet (object 8) in the direction it's facing by 2 units. This code needs to be after the "if spacekey..." command. Your code should look like this:



Now we have encountered a problem, when you run the program if you keep pressing the spacebar the bullet will keep resetting its position. In order to fix this we need to add some code onto the "if spacekey()=1" line. This code is:



This will make the program check that the spacebar is pressed AND the shoot# variable = 0. If either of these does not return a result of true, the commands will not be executed.

The last thing we need is some code to set the shoot# variable to 0 when it hits a wall, the code we need is this:



The "object collision()" command is there to return a value if two objects are overlapping in 3d space. The first number in the brackets is the number of the first object you want to check and the second number is the number of the second object you want to check. If you use two numbers over 0 i.e. 4,6 the command will return a 1 if there is a collision. In this case, where I have used a 0 for the second number the command will return the number of the object that the first object is colliding with, if any. I used the > expression so that this command will = true if the bullet is colliding with anything. Your code should now look like the following:



Now that is the shooting covered. Tomorrow I'll post about some collision *shudders* - collision took me about a week to get some decent code and decipher it. As allways if there is anything wrong with this code post and I'll see to it (I tested it on my other computer and all seems well although I may have missed something when doing any editing).

Anyway, Good luck for now. Cheers.

If I were you and you were me, then you'd be yourself 'cos you are me and I am you.
Opus
19
Years of Service
User Offline
Joined: 10th Jan 2005
Location:
Posted: 2nd Feb 2005 04:46
Is it too soon to recommend a sticky for this thread? You are doing great work Durdge39 and offering a valuable service for the newbie community.

Eternal student in search of knowledge. But will settle for the occasional epiphany.
Data
19
Years of Service
User Offline
Joined: 26th Nov 2004
Location: Winnipeg,Canada
Posted: 2nd Feb 2005 07:00 Edited at: 2nd Feb 2005 07:00
Great job!
Foxy
19
Years of Service
User Offline
Joined: 4th Jan 2005
Location: The Dale, South Australia
Posted: 3rd Feb 2005 20:13
@all others: Thanks for the comments.

Here is the last chapter to this series: collision.

Collision is definately one of the stumpers for those who haven't conquered it yet.

To be able to do collision in an FPS style game like this There needs to be two objects to collide. At the moment, we move the camera around with the arrow keys, and seing that the camera does not get involved in collision we need an object to act like a player object. This is easy, because all we need to do is make a sphere, just insert this code in our pre-game precedures:



As we all know this will make a sphere 10 units in diameter. Next we need to set up a variable that will control the angle that the object facing. Insert this code with the other variable setup:



This creates a variable for the angle named a# and its value is set to 0. Now we have those things set up we need to be able to control the sphere, or player object. To do this all we need to do is replace:



The move object command is exactly like the "move camera" command although this moves 3d objects, the first parameter is the object nmber that you wish to move and the second is how far you want to move it. The "a#=a#-2" command simply subtracts two from the a# variable, and vice-versa for the "a#=a#+2" command. The updated code should look like:



Now that will move the sphere back and forth but not rotate, nor does the camera follow the player object. The code below does just that:



The first line is a command that keeps a variable between 0 and 360, before the = sign you put the destination variable and between the brackets you place the variable you wish to be converted. The "position camera" command will merely position the camera in 3d space. We are using the "object position x()" etc. command to return the currnt position of the specified object along each axis. You know about the third line except we're using a variable as the angle parameter instead of a strait number. The "yrotate camera" command rotates the cam along its y axis (vertical), once again we are using the a# variable as a parameter instead of a fixed number. The latest code should look like:



Now for our collision code to work we need some more variables set up the first set is:



These variables will store the old position of the player object before any repositioning is done. These variables need to be placed at the start of the main loop as they need to be constantly updated.

The second set is:



These variables will store the position of the player object after the repositioning is done so this set should be put after those if...then statements. The code should now look like this:



To keep things tidy the "gosub" command is used. This command makes the program skip to another place in the code. The syntax for using this command is:



The place that the program skips to must be marked with a colon ( at the end of the label. The return command defines the end of that sub, this command simply makes the program go back to where it left and continue on normally.

To make our code happen there needs to be an if statement in the middle of our code that looks like this:



I set it so that it would work when it returned a value greater than 1 because the gun is object no. 1. If it was collisiong with this then you wouldn't be able to move.

Also this code needs to go after the main loop:



The new code should look like this:



Now, time for the heart of the collision the code that makes everything happen is this:



I know it might seem baffaling but what it does is it repositions the object to its original x position and if that doesn't work it'll try the y posision and so on. The else statements mean that if an if command results in a false it will do the things between the else and the endif statements instead. This code may take a while to study. This needs to be placed between the collision: and the return commands.

Finally we need to update the position of the object, all we need to do is insert this code into our main loop:



Also, to fix a shooting problem that occurs insert this code into the "if spackey()" set:



The code should look like this:



And thats it! I'd like to thank Lost In Thought and G-Man for that collision code, it is brilliant.

Anyway, keep programming!

If I were you and you were me, then you'd be yourself 'cos you are me and I am you.
Data
19
Years of Service
User Offline
Joined: 26th Nov 2004
Location: Winnipeg,Canada
Posted: 3rd Feb 2005 23:44
Great!
Cool toturial!
Opus
19
Years of Service
User Offline
Joined: 10th Jan 2005
Location:
Posted: 4th Feb 2005 03:21
Outstanding Durdge39! Thanks. Also, thanks to Lost In Thought and G-Man for the collision code.

Opus

Eternal student in search of knowledge. But will settle for the occasional epiphany.
Freddy 007
19
Years of Service
User Offline
Joined: 30th Nov 2004
Location: Denmark
Posted: 4th Feb 2005 03:52
good job Durdge39!!

I think someone should sticky this. It might become VERY handy to other newcomers. FPS games is surely becoming more and more popular.
sticky it

And Durdge39: thanks man, this is a cool tutorial


Check out PanzerGames at http://www.freewebs.com/panzergames
Foxy
19
Years of Service
User Offline
Joined: 4th Jan 2005
Location: The Dale, South Australia
Posted: 4th Feb 2005 05:50
Thanks guys.

If I were you and you were me, then you'd be yourself 'cos you are me and I am you.
Baggers
20
Years of Service
User Offline
Joined: 31st May 2004
Location: Yonder over dem dere hills
Posted: 4th Feb 2005 09:19
Yep definatly sticky worthy
<Puts ear to ground and waits for the footfalls of the mighty mods>
Sol462
20
Years of Service
User Offline
Joined: 12th Sep 2004
Location: playing with the spazookeedoo
Posted: 5th Feb 2005 11:12 Edited at: 5th Feb 2005 11:13
and next, we can do a tutorial on mmorpgs!!!1!!11!one
just joking, but hey, it could end the annoying mmorpg threads.
*wonders*...
anyway, great tutorial drudge! very very useful.

coffee + monkeys + creativity = games
Operation Pineapple - Multiplayer FPS WIP
BLink
20
Years of Service
User Offline
Joined: 1st Jan 2004
Location: Laptop, wherever that is
Posted: 13th Feb 2005 14:31
cool! this has helped me alot with my own shooter space flight simulator. But I didn't see one thing, and that was multiple bullets at the same time. Can you show us how to do that too? That would really help out a lot, since I'm kind of baffled on how to get to there from here.

www.gameroom.com/blinkcomic
Foxy
19
Years of Service
User Offline
Joined: 4th Jan 2005
Location: The Dale, South Australia
Posted: 13th Feb 2005 17:12
Thanks for the feedback peoples.

Well what you could do is copy the code for the first bullet and and use it for (x) amount of bullets and then have some code to check which bullets aren't in motion when you hit the fire key and fire the first bullet it finds to be non-active but I'm sure this would hit the performance somewhat - like halve your fps, if not worse. That's about all I can offer at the moment as I'm still a newbie.

If I were you and you were me, then you'd be yourself 'cos you are me and I am you.
BLink
20
Years of Service
User Offline
Joined: 1st Jan 2004
Location: Laptop, wherever that is
Posted: 14th Feb 2005 06:05
heh, alright, I'll ask in a new topic then since most people will look at this and say "Oh! That problem looks solved!" and its about something different anyways.

www.gameroom.com/blinkcomic
Gil Galvanti
19
Years of Service
User Offline
Joined: 22nd Dec 2004
Location: Texas, United States
Posted: 14th Feb 2005 10:13
(chanting) sticky! sticky! sticky!

Video games…they can take you places unreachable, impossible, unfeasible. They put you in the book...they put you in the movie...they put you in a world, a world that before could only be imagined.

Login to post a reply

Server time is: 2024-09-23 12:19:52
Your offset time is: 2024-09-23 12:19:52