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 Discussion / Collision Problem

Author
Message
dmurphy
14
Years of Service
User Offline
Joined: 2nd Oct 2009
Location:
Posted: 2nd Oct 2009 22:16
Hey my game is a physics game and so far i've made Earth and the Sun. I'm trying to make it so that if the ship (a cube) hits the sun or the earth it goes back to the starting point. The problem is that it works with the sun but not the earth. Any suggestions?
[sync on
sync rate 60
autocam off
randomize timer()
hide mouse
backdrop off

load image "images/space.jpg",1
load image "images/explosion.jpg",2
load image "images/earth.jpg",3

MySpeed = -25
MyAcceleration = 0.0

fuel = 100

set camera range 20,1000000

make object cube 1,5
set object collision on 1
set object collision to boxes 1

make object sphere 4,-1000000
texture object 4,1

rem Planets
for t = 1 to 2
make object sphere t + 1 ,1000
set object collision on t + 1
set object collision to spheres t + 1
next t

texture object 2,2
position object 2,10000,0,0

texture object 3,3
position object 3,2500,0,0
scale object 3,25,25,25

MainSection:
do
set cursor 10,10
print "Fuel:",fuel
box 10,30,fuel*6.3,40

if upkey()=1 then pitch object down 1,1
if downkey()=1 then pitch object up 1,1
if leftkey()=1 then turn object left 1,1
if rightkey()=1 then turn object right 1,1
if spacekey()=1 then move object 1,0-MySpeed
if inkey$() = "a" then roll object left 1,1
if inkey$() = "d" then roll object right 1,1
if inkey$() = "s" then move object 1,MySpeed

position camera object position x(1),object position y(1),object position z(1)
set camera to object orientation 1
pitch camera down 20
move camera -25

if fuel < 0 then fuel = 0

if fuel > 0
if inkey$() = "l"
ghost object on 4
dec fuel
myspeed = -100
endif
endif

if fuel = 0
ghost object off 4
myspeed = -25
endif

for t = 1 to 2 : rem If I hit one of the little fruits
if Object collision(1,t + 1)>0
position object 1, 0, 30, 300
endif
next t

sync
loop
]

Candy = good!
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 3rd Oct 2009 13:34
Hello!

Please use code boxes... They make it much easier to read your code:



Just highlight your code and hit the "code" button in the message edit box.

Indenting your code will help you a lot too. Your code looks something like this:



I indent it after every if, do, for, while, repeat, open to read, open to write and else. Doesn't it look better this way?



You only need to add comments and then your code will be perfect:



Now, to your problem.

I can't quite see why it isn't working, since I haven't approached collision with DBC commands for a long time. However, I suggest to try the following:



I think that code is causing it. You should set up collision after you set your objects size and position. So scaling object 3 might be disabling the collision or doing something weird to it.

I would also try checking if your ship is really heading towards the planet it should be colliding with.

Also try setting the collision off for object 4, your sky:



I hope you find the error and I could help!

TheComet


Make the paths of your enemies easier with WaypointPro!
dmurphy
14
Years of Service
User Offline
Joined: 2nd Oct 2009
Location:
Posted: 7th Oct 2009 05:19
i tried but it didnt work so this is what i have now after trying

REM *****************************************
` *** TITLE - The Theoretical Universe
` *** VERSION - 1.0.0 LAST UPDATED - 10.5.2009
` *** DEVELOPER - Andrew Murphy
` *** DATE CREATED - 9.22.2009
` ***
REM *** START SYSTEM SETUP SECTION



i dunno whats wrong

also attached is the model of the ship

Candy = good!

Attachments

Login to view attachments
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 8th Oct 2009 02:31 Edited at: 8th Oct 2009 02:35
Like thecomet said, scaling an object can mess up the collision.

Here's a few guide lines to follow:

* For whatever object(s) you want to have collision, always use SET OBJECT COLLISION ON <object number>

* After object collision is turned on, then set the type of collision : BOXES, SPHERES, POLYGONS.

* For any objects that you do not want to have tested for collision, SET OBJECT COLLISION OFF <object number>

* Don't use SET GLOBAL COLLISION OFF or SET GLOBAL COLLISION ON . They are buggy commands and may not behave as expected.

* If and After you scale an object, turn collision OFF, then turn collision ON, then set it's collision to boxes, spheres or polygons.

* An alternative to using SET OBJECT COLLISION TO BOXES ,POLYGONS, or SPHERES, is MAKE OBJECT COLLISION BOX. It tends to be much faster for collision testing - though it can be less accurate for objects with many limbs. Also be aware that MAKE OBEJECT COLLISION BOX collision will only be tested against other objects that have used MAKE OBJECT COLLISION BOX or areas enclosed in a static collision box.

As far as your code, you may want to be careful with gigantic camera ranges like

set camera range 20,1000000

Also be aware that the 20 means anything closer than 20 units to the camera will not be drawn and 1000000 is a very far distance for rendering.

For your collision test:



perhaps try setting the return of object collision() to a variable:


Or instead of testing each object 1 at a time, you can use a collision test with all objects coliding with object 1 by using object collision(1,0):


[EDIT]
One other thing, this:

is an error. THEN should not be at the biginning of the next line if you are using and if endif block. THEN only appears if the IF clause is on the same line:


Your original block should read:


Enjoy your day.

Login to post a reply

Server time is: 2024-05-03 20:31:16
Your offset time is: 2024-05-03 20:31:16