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 Professional Discussion / Why is my game so slow?

Author
Message
New Coder Skilless
13
Years of Service
User Offline
Joined: 4th Oct 2012
Location:
Posted: 4th Oct 2012 17:26
I'm new to DBPro but have some coding experience.

I'm making a small scale physics based game (5 levels) as part of a project for college.

For now, I'm using the basic objects that come with DBPro (cubes, spheres, etc).

At load, it creates 1000 cubes and puts them randomly in the map.
Now, I can make all 1000 of them fall with gravity, and it's very smooth - about 120FPS.
As soon as I try to check for them colliding with each other, it goes really slow - about 14FPS.

My code:



My PC is:
OS: Windows 7 Pro 64Bit
Processor: Intel Xeon @ 2.4GHz (2 Processors)
RAM: 16GB
HDD: 1TB
Graphics: Radeon X1550

Runs Portal on max settings at 60FPS

Any help and advice is hugely appreciated.
Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 4th Oct 2012 18:09
Checking 1000 cubes for collision with each other, that's 999 collision checks for each cube. So every loop you're doing 999000 checks. My thought is that nearly a million collision checks per loop would slow down any system.

"You're not going crazy. You're going sane in a crazy world!" ~Tick
Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 4th Oct 2012 18:15 Edited at: 4th Oct 2012 20:04
At a glance, it seems like the collision is nested , so 1000 objects actually means 1000*1000 collision comparisons.

This section,



You can halve this by making the inner loop start from the outer loops current item and loop through the end.



A much better option would to be look into some type of spatial partitioning / zone classification. Where the code only compares a short list of objects that share the same zone, rather than every object regardless of where it is in space.

The Weeping Corpse
14
Years of Service
User Offline
Joined: 19th Sep 2011
Location: United Kingdom
Posted: 4th Oct 2012 20:38
You will need to implement some kind of spatial partitioning technique. Each cube only needs to be tested with other cubes that are close by, otherwise you are wasting CPU cycles by testing cubes that can never touch each other.

The most basic technique is to fit an imaginary sphere around each cube. If the distance between the centers of any two spheres is greater than the diameter then the enclosed cubes can't possibly be in contact with each other.

Mage
Valued Member
19
Years of Service
User Offline
Joined: 3rd Feb 2007
Location:
Posted: 6th Oct 2012 04:38
Quote: "The most basic technique is to fit an imaginary sphere around each cube. If the distance between the centers of any two spheres is greater than the diameter then the enclosed cubes can't possibly be in contact with each other."


No this is a place where you can cut corners and win. Use a cube instead of a sphere, just make that cube big enough to house the on screen cube with all it's rotation.

This way you can skip using the SQRT() function and distance checks. Just check if other imaginary cube is within a certain range of X, range of Y, range of Z. It's quicker.

TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 7th Oct 2012 00:24
Quote: "This way you can skip using the SQRT() function and distance checks. Just check if other imaginary cube is within a certain range of X, range of Y, range of Z. It's quicker."


NOOOOOO, distance checks are much faster than cube checks if you use the fast distance check.

Instead of:



You can use:



TheComet

"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
Mage
Valued Member
19
Years of Service
User Offline
Joined: 3rd Feb 2007
Location:
Posted: 7th Oct 2012 04:03
Quote: "NOOOOOO, distance checks are much faster than cube checks if you use the fast distance check."


Leaving the values squared and checking the squared values instead? Sure I actually do this.

I was a little hesitant to suggest it to other people because I am worried about possible overflow errors. You don't know what kind of numbers people are going to plug into your code... so maybe 100,000 or a million squared. Starts getting kind of high. I can honestly say I thought about this before my post, but decided to play it safe.

Login to post a reply

Server time is: 2026-07-07 19:10:59
Your offset time is: 2026-07-07 19:10:59