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 / Coordinates being not updated

Author
Message
chafari
Valued Member
20
Years of Service
User Offline
Joined: 2nd May 2006
Location: Canary Islands
Posted: 23rd Nov 2011 14:30
Hi there. I'v got a piece of code to check what object is the nearest to xx and it works ok the first time, but when the objects change position, the coords are wrong. That's driving me crazy..I can't see what's wrong...could someone enlighten my way !!



Cheers.

I'm not a grumpy grandpa
WLGfx
18
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 23rd Nov 2011 14:43 Edited at: 23rd Nov 2011 14:44
Move:

just before the first for next loop...



Mental arithmetic? Me? (That's for computers) I can't subtract a fart from a plate of beans!
Warning! May contain Nuts!
zenassem
23
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 23rd Nov 2011 14:46 Edited at: 23rd Nov 2011 15:02
On quick glance, I think that it has to do with the fact that you never reset distance#.

So after the first object is less than distance#... distance gets set to that object... distance#=dist#(i) and near=i

Now, that near object gets moved somewhere else. So you won't see the next closest object after the move, unless an object gets closer than the previous "closest" object was. Hence, you are setting up a whoever got the closest record, rather than clearing past results.

I'll have to run itto see if something else is going on though.
[Testing project].....

~ZENassem
zenassem
23
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 23rd Nov 2011 14:55
I think it's working. I tested by adding distance#=10000 before the loop. You can probably employ a better method to pick the next nearest,, but at least this may get your engines firing again.



~ZENassem
chafari
Valued Member
20
Years of Service
User Offline
Joined: 2nd May 2006
Location: Canary Islands
Posted: 23rd Nov 2011 14:57
I have to admit, that the oldest, I sharpen the alzeimer. I put the variable out the main loop!!...I'm a f.. fool what would be next ???

Thanks guys . WLGfx you are my heroe

Cheers.

I'm not a grumpy grandpa
zenassem
23
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 23rd Nov 2011 15:10 Edited at: 23rd Nov 2011 15:12
I also noticed that if the occurence comes up that more than one object computes to the same distance away; your algorithm will choose the first object number (the lowest from 1 to 20) and report that as being the definitive closest, ignoring any (higher number) objects that tied.

~ZENassem
chafari
Valued Member
20
Years of Service
User Offline
Joined: 2nd May 2006
Location: Canary Islands
Posted: 23rd Nov 2011 15:33
@zenassem
Quote: "your algorithm will choose the first object number (the lowest from 1 to 20) and report that as being the definitive closest, ignoring any (higher number) objects that tied"



Well, what I was trying with this example, was exactly that, to get just the nearest object, so just pointing and moving the searching object to this direction,when both objects collide, this object will be place far away to force the searching object to go for the next way point object.

Cheers.

I'm not a grumpy grandpa
WLGfx
18
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 23rd Nov 2011 18:37 Edited at: 23rd Nov 2011 18:40
@chafari - If you're testing lots of distances then don't use the sqrt() function til the very end. I don't have an example in DBPro but from my latest game I'm working on I have this from my current game.



This 'dist' in my case is the dx+dy maximum value that could possibly be. Basically what I'm doing is checking upto 12,000 objects and not using the sqrt() function til I've actually found the closest which shaves a massive amount of CPU time off the check. I'm going to be changing the above function to 'not' use the STL library too to speed it up even more.

Hope this helps...

EDIT: Notice that I've also used ints for the main function as they calculate faster.

Mental arithmetic? Me? (That's for computers) I can't subtract a fart from a plate of beans!
Warning! May contain Nuts!
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 23rd Nov 2011 19:04
I haven't checked the whole code but why take square roots at all if you only need to find the closest? In other words, it'll be the closest whether you work with distance or distance squared.
chafari
Valued Member
20
Years of Service
User Offline
Joined: 2nd May 2006
Location: Canary Islands
Posted: 23rd Nov 2011 20:18 Edited at: 23rd Nov 2011 20:41
Quote: "@chafari - If you're testing lots of distances then don't use the sqrt() "


Quote: "I haven't checked the whole code but why take square roots at all if you only need to find the closest?"


You are both right !!, It was just an example I tested, but actually I use vector. Something like:



Edited.
I'm not quite sure which method is fastest. Time ago I found this comparison code.



Cheers.

I'm not a grumpy grandpa
WLGfx
18
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 23rd Nov 2011 21:11
The fastest method before using a sqrt() function is probable using the (dx^2)+(dy^2) as it's only using 3 calcs in the formula and also not the long winded sqrt() function.

Because I'm not after precision accuracy and can be 0.1 either way of the final result I use this:



Which is 1/50th of the speed of the normal sqrt() function, which is a total of 6 calcs and still gives a 'gamers' accuracy.

Mental arithmetic? Me? (That's for computers) I can't subtract a fart from a plate of beans!
Warning! May contain Nuts!

Login to post a reply

Server time is: 2026-07-10 03:59:42
Your offset time is: 2026-07-10 03:59:42