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 / [DBP]Creating objects one ata time -- stuck!

Author
Message
Reynard
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: CT, USA
Posted: 23rd Jan 2007 03:01
First of all, I am new to both DarkBasic and programming in general. Second, I am not begging for the 'teh codez'. Really, I am hoping for someone to point me in the right direction so I can figure it out (mostly) for myself -- else I don't learn a whole lot.

Anyway, I have tried a number of permutations on my problem and none of them seem to work. So I will lay it out and see what you think.

My intent is to create what might be the core to a 3d space shooter. Basically, I am trying to get rotating cubes (stand ins for asteroids) to appear at random places on the screen, one at a time, and have them move "forward" toward the player. (Later, I will worry about destroying them, since I think I got a decent handle on that from Ruccus' FPS tut).

I first tried using a for...next to create the objects, then another for...next to move/rotate them. Unfortunately, it created all the objects, then moved them all in unison. I triedto add a countdown# variable to the creation loop in hopes it would 'move on' to the movement part before creating another cube. no dice.

Here's the code for what I managed to do:


sync
hide mouse
color backdrop rgb(0,40,0)

randomize timer()

countdown#=100

do

dec countdown#,1

if countdown#=0
for n=1 to 10
make object cube n,25
color object n,rgb(rnd(255),rnd(255),rnd(255))
position object n,rnd(640),rnd(480),1000
countdown#=100
next n
endif

for n=1 to 10
if object exist(n)
xrotate object n,object angle x(n)+2
yrotate object n,object angle y(n)-2.5
position object n,object position x(n),object position y(n),object position z(n)-2
endif
next n

sync
loop
[\code]

also -- I can't seem to get the cubes to appear anywhere but the upper right quadrant of the screenand I am not sure why.

Anyway -- thanks for any insights. like i said, pointing me in the right directions is as good or better than shoving code at me (though i will take that too).
Richard Davey
Retired Moderator
22
Years of Service
User Offline
Joined: 30th Apr 2002
Location: On the Jupiter Probe
Posted: 23rd Jan 2007 03:29 Edited at: 23rd Jan 2007 03:30
Oohhh.. SO close

The trick here is that you want to use the count-down timer, but when the count-down is reached you then want to create ONE object, and re-set the counter back up again. Then repeat.

So don't use a 'for next' loop at all - just create one single object per count-down. Here's some pseudo code to explain:



The above is just pseudo code to give you an idea of the logic you could use. There are a hundred ways to skin this cat, but the above should get you going.

Cheers,

Rich

Heavy on the Magick
Flashing Blade
21
Years of Service
User Offline
Joined: 19th Oct 2002
Location: United Kingdom
Posted: 23rd Jan 2007 14:16
Quote: "also -- I can't seem to get the cubes to appear anywhere but the upper right quadrant of the screenand I am not sure why."


That's because:

Quote: "position object n,rnd(640),rnd(480),1000"


3D co-ordinates are not same as 2d co-ords.

0,0,0 is centre of 3D world

So you wsnt your x to be something like random from -200 to 200 (x=rnd(400)-200), your y and your z similiar. Experiment with the ranges until you get what you want.


The word "Gullible" cannot be found in any English Dictionary.
TDK
Retired Moderator
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 23rd Jan 2007 14:56
And, if you don't use AutoCam Off, the camera will always point at the last created object.

If you do use AutoCam Off (as you really should do), you are responsible for positioning and pointing the camera as it will by default point at 0,0,0.

As Flashing Blade says, because your random values will all be positive along the X and Z axis and your camera is pointing at 0,0,0 the objects will all be above and right of where the camera is pointing.

Use the method he suggests to distribute the object's positions randomly around 0,0,0 as it's easier than calculating the position of the middle object and pointing the camera at it.

TDK_Man

Reynard
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: CT, USA
Posted: 23rd Jan 2007 23:58
I got it to work, at least insofar as the objects appearing when and where they should. (I still need to toy with camera and object location and object speed, etc..)

However, it seems that in order to get the cubes to fly at the camera (aka the player) they stop rotating (since the only way I know how to do it is the "point object" command).

Here's the working code as it stands:



Also: since my eventual plan is to have the player sit there and blast at flying, rotating cubes as they fly at the player, what's a good way to restrict both the spawn points of the cubes and the angle of the camera? I am thinking I want a semi-sphere "in front" of the camera. Can I use define a radius for the random spawn points? Can I just stop the mousex and mousey at +/-90 degrees?

Thanks.
Flashing Blade
21
Years of Service
User Offline
Joined: 19th Oct 2002
Location: United Kingdom
Posted: 25th Jan 2007 14:25 Edited at: 25th Jan 2007 14:31
Quote: "However, it seems that in order to get the cubes to fly at the camera (aka the player) they stop rotating (since the only way I know how to do it is the "point object" command)."


You need to have control of each astroid each with its own data.
UDT's and arrays are best. Here's a quick example of your program using a user defined type and an array to control all astroids



That code is just to demonstrate how to control many different objects each with their own data.
The code cheats by using the point object and move object. Instead you are best in the function makeastroid() determing the x,y,z velocities from the x,y,z angles and speed variables. These can then be stored in the udt and used to move astroid. eg:
In the makeastroid() function:
position astroid
point astroid
use either trig or 3d vector maths to calculate velocities.
Now in moveastrroids() function add velocities to x,y,z and position object.
This method will be better in controling the astroids, eg like when two astroids collide you can calculate new velocities so they bounce away from each other.

I also altered your countdown code slightly - the method you were using would be different on deifferent speed computers.


The word "Gullible" cannot be found in any English Dictionary.

Login to post a reply

Server time is: 2024-09-25 17:24:14
Your offset time is: 2024-09-25 17:24:14