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 / Collision (I tried a search!)

Author
Message
AlexK
16
Years of Service
User Offline
Joined: 23rd Mar 2008
Location:
Posted: 5th Apr 2008 01:52
Hello
Sorry, another collision question... I know the board is filled with them but I searched and didn't find.

I'm adding NPCs to a little RPG engine like baldurs gate/neverwinter night. I need to check if my player is close enough to a NPC to speak with him.
I don't really care if he's facing the NPC or not. I just want to see if he's close enough.

So I was wondering how to do this?
I tried using sphere Casting but it didn't work and doesn't make much sense since I don't want to cast the sphere in one direction but instead search all around my player for collision.

I also tried drawing a sphere around the player and using the built-in "object collision" command but this only works if the edge of the sphere is in collision with the NPC. If the NPC is inside the sphere then it doesn't detect it.

Can anyone offer a little advice?
Gil Galvanti
19
Years of Service
User Offline
Joined: 22nd Dec 2004
Location: Texas, United States
Posted: 5th Apr 2008 01:57
Look up the distance formula.


AlexK
16
Years of Service
User Offline
Joined: 23rd Mar 2008
Location:
Posted: 5th Apr 2008 02:03

Sorry. I've been up too long!
I've spent an hour trying to get the thing done with collision and didn't even think about using distance >.<
HowDo
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 5th Apr 2008 07:28
Also look up

1. SET OBJECT RADIUS Object Number, Radius
2. SHOW OBJECT BOUNDS Object Number, Box Only Flag


might make your first way work.

Dark Physics makes any hot drink go cold.
Twinsen
18
Years of Service
User Offline
Joined: 20th Jun 2006
Location: Romania
Posted: 6th Apr 2008 12:52 Edited at: 6th Apr 2008 12:52
yes, use the distance ... if you don't remember how to do it, here's the formula:

distance = sqrt ( sqr ( X1 - X2 ) + sqr ( Y1 - Y2 ) + sqr ( Z1 - Z2 ) )

Could you help me treat my injured Dino-Fly ?
TechLord
21
Years of Service
User Offline
Joined: 19th Dec 2002
Location: TheGameDevStore.com
Posted: 6th Apr 2008 13:16 Edited at: 6th Apr 2008 13:25
For performance reasons, you could also use a Heuristic:

This formula only takes in consideration of gameplay on 2 planes such as Terrain.

Twinsen
18
Years of Service
User Offline
Joined: 20th Jun 2006
Location: Romania
Posted: 6th Apr 2008 13:31
well my formula works for all 3 planes ...

Could you help me treat my injured Dino-Fly ?
TechLord
21
Years of Service
User Offline
Joined: 19th Dec 2002
Location: TheGameDevStore.com
Posted: 6th Apr 2008 18:49 Edited at: 6th Apr 2008 19:09
Quote: "well my formula works for all 3 planes"
You can easy add the Y plane in the Heuristic calculation.

Quote: "If Abs(Player.X - NPC.X) + Abs(Player.Z - NPC.Z) + Abs(Player.Y - NPC.Y) < Acceptable_Range_Cost Then Player_Within_NPC_Chat_Range"


The Pythagorean Theorem requires more calculation and could impact performance. If that becomes an issue, a Heuristic can provide a faster approximation for the distance.




The decision to use one or the other would be on based precision-vs-performance.

Twinsen
18
Years of Service
User Offline
Joined: 20th Jun 2006
Location: Romania
Posted: 6th Apr 2008 20:21
hmm well the heuristic, from what I can see, works by a certain "range" in all directions, rather than a precise distance point ... your formula is cool I'll try it sometimes

Could you help me treat my injured Dino-Fly ?
Sasuke
18
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 6th Apr 2008 20:53
Don't really understand what the results of Heuristic are, they seem to be neally double of any other distance formula, that can't be right can it?
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 6th Apr 2008 21:13
It seems to me that this formula would give a better answer:

If Abs((Player.X + Player.Y + Player.Z) - (NPC.X + NPC.Y + NPC.Z))< Acceptable_Range_Cost Then Player_Within_NPC_Chat_Range"

LB
Twinsen
18
Years of Service
User Offline
Joined: 20th Jun 2006
Location: Romania
Posted: 6th Apr 2008 21:58
yes, that averages things up more !!!

Could you help me treat my injured Dino-Fly ?
Sasuke
18
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 6th Apr 2008 23:42 Edited at: 6th Apr 2008 23:42
What? this returns the wrong value. That would just add all the values together.

Example:
Say the play is positioned: x = 500 , y = 500 , z = 500.
If the position of the npc was at 0 on all axis', using your formula would return 1500. You know straight away it should be under 1000(or the actual answer 866.025).
KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 6th Apr 2008 23:58
Stick with,

Dist = sqrt(((X1-X2)^2) + ((Y1-Y2)^2) + ((Z1-Z2)^2))

It's a well established distance formula, and performs well.

TechLord
21
Years of Service
User Offline
Joined: 19th Dec 2002
Location: TheGameDevStore.com
Posted: 7th Apr 2008 00:53 Edited at: 7th Apr 2008 01:30
Quote: "Dist = sqrt(((X1-X2)^2) + ((Y1-Y2)^2) + ((Z1-Z2)^2))

It's a well established distance formula, and performs well."
I threw the heuristic out there as a option. You may find it useful for other distance/approx calculations.

I honestly dont see a problem with the using the Distance Formula with a low number of checks per Game loop. However, if Game Engine has to perform 100's or 1000's of Distance Checks, you may want to use a heuristic to approximate wether or not the more precision check with Distant Formula is needed. This can serve as a way to scope down the processing and increase performance by using the precision only when needed.

KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 7th Apr 2008 01:34
True enough. It does depend on the accuracy required. At a certain distance accuracy might be less important, where an approximation might do.

I don't have the time at the moment to throw together a test to see how accurate it is. The amount of accuracy would be important, and you would have to run some tests to find out at what point does the accuracy become an issue, at which point you would switch to the Sqrt formula.

It's worth trying anyway.

Sasuke
18
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 7th Apr 2008 12:56 Edited at: 7th Apr 2008 12:59
Or I could tell you how accurate it is.

If you position the the player at any distance from the camera, stuff it, heres a picture...




I hoping that I finally get the point the across. If it's speed you want, use vectors cause there just a fastest accruate distance formula.

Edit, if you positioned the npc on the oppisite side you would get 512.0.

Attachments

Login to view attachments
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 7th Apr 2008 13:11
You make a valid point. The numbers I was using for comparison were quite different. I do agree with Techlord, in that when many checks are needed an alternative should be sought, and vectors is a good idea.

Best,

LB
TechLord
21
Years of Service
User Offline
Joined: 19th Dec 2002
Location: TheGameDevStore.com
Posted: 7th Apr 2008 15:35
@Sasuke

Not sure what formula your using for your heuristic, but if your using the one I posted and pluged in the Player Position (0,0) and NPC Position (20,20) --> Abs(Player.X - NPC.X) + Abs(Player.Z - NPC.Z)

Abs(0 - 20) + Abs(0 - 20) =
Abs(20) + Abs(20) =
20 + 20 =
40
heuristic = 40

KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 7th Apr 2008 19:29 Edited at: 7th Apr 2008 19:32
There was a thread in the DBPro forum about the sqrt formula vs. vectors. The sqrt formula is faster.

A better solution would be to use a timer to check for distance at certain intervals. As you draw closer to the object in question then reduce the amount of time between checks.

If you're (speaking relative to scale of course) 1000 feet away from an NPC that you might be able to talk to, it doesn't make sense to check the distance every loop until you're maybe 50 or 20 feet away.

The timer DLL in the Matrix1Utility pack is very handy for this.

TechLord
21
Years of Service
User Offline
Joined: 19th Dec 2002
Location: TheGameDevStore.com
Posted: 8th Apr 2008 00:40 Edited at: 8th Apr 2008 01:08
I will eventually have to deal with this issue in my project. So I took some time out to write a code snippet. As the snippet will demonstrate the Heuristic (Distance Approximation) can be useful where precision is not a necessity. IMHO, precision is not required for determining the distance between the player and NPC for dialog. However, precision is required in combat between the player and NPCs.



Quote: "A better solution would be to use a timer to check for distance at certain intervals. As you draw closer to the object in question then reduce the amount of time between checks."
Combined this with Adjustable Precision. Hmmm.

LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 8th Apr 2008 02:02
Nice comparison. Why not throw in a vector check while we're at it?


LB
Sasuke
18
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 8th Apr 2008 02:09
Sorry TechLord, I was talking about this formula:

Abs((Player.X + Player.Y + Player.Z) - (NPC.X + NPC.Y + NPC.Z))

Yours works though, but some of the results, I think are abit manic, but thats just me. But even using that, vectors are still faster than any other and you get precision.

And i'll show the difference in speed, using Mr Kohlenstoff ideas of returning the speed, I put this together:


Your notice that GetSQRT_Distance3 is as basically as fast as vectors, though vectors 9/10 is the fastest.
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 8th Apr 2008 02:57
Not to beat a dead dog here, but how do you figure
Quote: "If Abs(Player.X - NPC.X) + Abs(Player.Z - NPC.Z) + Abs(Player.Y - NPC.Y) < Acceptable_Range_Cost Then Player_Within_NPC_Chat_Range""
will come up with a better answer to
Quote: "Say the play is positioned: x = 500 , y = 500 , z = 500.
If the position of the npc was at 0 on all axis', using your formula would return 1500."
??

Abs(500 - 0) + Abs(500 - 0) + Abs(500 - 0) = 1500
Sasuke
18
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 8th Apr 2008 03:10
No, this forumla doesn't work correctly:

Abs((Player.X + Player.Y + Player.Z) - (NPC.X + NPC.Y + NPC.Z))

But this does:

Abs(Player.X - NPC.X) + Abs(Player.Y - NPC.Y) + Abs(Player.Z - NPC.Z)

Thats all I meant.


Did you try my snippet btw, what results did you get?
TechLord
21
Years of Service
User Offline
Joined: 19th Dec 2002
Location: TheGameDevStore.com
Posted: 8th Apr 2008 04:18
Sasuke, I tried your speed comparison code, I'm shocked at how slow a ABS call is

LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 8th Apr 2008 04:36
I ran it about 20 times and got the following results:
SQRT calc = 62 to 78ms
faster SQRT calc = 47 to 78 ms
much faster SQRT calc = 15 to 32ms
Heuristic = 31 - 47ms
Sasuke's vector calc = 16 (mostly), but occasionally 31-32.

LB
Sasuke
18
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 8th Apr 2008 04:46 Edited at: 8th Apr 2008 04:46
@TechLord, I thought it was a go idea to get a approximation though, but since it not as fast as vectors, you would just have to do so many checks per so many loops depending on distances. I use grid(or cell) system for stuff like this. Anything in the primary cell(or camera cell) gets top checks. Secondary cells get less checks and so on. Then you have to account for object type and scale, but thats for LOD stuff.

@LBFN, we get around the same results and vectors still look like the way to go.
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 8th Apr 2008 04:56
@KISTech -
Quote: "There was a thread in the DBPro forum about the sqrt formula vs. vectors. The sqrt formula is faster."


Not in my tests... Please Show me. I'm baffled.

LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 8th Apr 2008 05:03
I thought that using ABS was faster than that too.

Quote: "vectors still look like the way to go. "


Yep. I just changed to vectors in a game I'm working on and it's working great. I see me using them a lot more in the future than what I have in the past. Thanks for sharing your insight.

LB
KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 8th Apr 2008 06:08 Edited at: 8th Apr 2008 06:13
Ok, I spent a little time with the code. While I really didn't change much, I wanted to make sure we were comparing apples to apples.



The original didn't change the object positions. Not that this makes a big difference for calculating time, but it shows the level of accuracy between the heuristic formula and the others.

I didn't change the code so that the objects could have negative positions, so I'm not really sure what impact that might have.

I'll have to find the thread, Jason, I think you were in that thread weren't you? I may have it backwards, but I could swear that the conclusion was that the sqrt formula was faster.

The reason for moving some things around is so that each subroutine is presented with the same data to work from, and no other commands or processes have to occur for them to do their calculation.

In my tests, the vector and the faster Sqrt functions performed the fastest. If you hit the space to run it about 20 times you'll notice that the two sort of flip-flop between 13-16 and 42-47. On my machine about 6 out of 10 times the sqrt3 function came out 13-16, while the vector function came out at 42-47.

I'm not sure why they flip around, but given that those two are the fastest using either one of them should be your best performance bet. They are close enough in performance that the difference is negligable.

Now if we could substitute a simpler formula for SQRT like was done replacing the X^2 with X*X.

Sasuke
18
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 8th Apr 2008 06:55 Edited at: 8th Apr 2008 06:56
This might be that page:
http://forum.thegamecreators.com/?m=forum_view&t=120418&b=7

Theres an interesting function in there that Cash Curtis made:


It returns a 1 if the object is within range and its damn fast, faster than any of these distance formula's. But what it doesn't do is return actual distance, so you would have to use a faster Sqrt formula or vectors to get actual distance.
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 8th Apr 2008 13:42
Quote: "I'll have to find the thread, Jason, I think you were in that thread weren't you? I may have it backwards, but I could swear that the conclusion was that the sqrt formula was faster."


Vectors faster, but there were some "alternative" distance functions that dont return distance but tell you if item within a distance - (according to that thread Sasuke posted)

Vectors distance checking is faster than SQRT being used to calc didstant.

You need to create a vector3 - in the top of your program, and reuse it to get the speed

(yawn) just woke up - no coffee yet - hope tis read s ok

TechLord
21
Years of Service
User Offline
Joined: 19th Dec 2002
Location: TheGameDevStore.com
Posted: 8th Apr 2008 14:12 Edited at: 8th Apr 2008 14:12
Vectors are now my choice for Distance Checks in DBPro. Fast and Accurate - no need to approximate. I'm using lots of distance checks in relation to camera for LOD, Logic, etc in my MMORPG Engine. This is definately a BIG help. Thanks guys.

KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 8th Apr 2008 18:23
Quote: "Theres an interesting function in there that Cash Curtis made:"


HOLD ON JUST A SECOND. This is BLOODY BRILLIANT!!



This allows you to at least check if the distance is within the range you want. If you need the actual distance at any given point then just,

dist# = sqrt(d#)

Here's the code plugged in to the test app. It consistently gives around 15-16, and occasionally returns 0ms.

I think I know what I'll be using.



Sasuke
18
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 8th Apr 2008 19:15
Quote: "I think I know what I'll be using."


Same here
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 9th Apr 2008 01:01
No defraction from Sasuke's code - but that test is skewed.

You Compared the Vector doing the FULL Distance Calc against the "approximation" that is NOT SQRT'd.


KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 9th Apr 2008 02:26
You only get the SQRT of it when it's needed for actual distance. (like when I displayed the distance)

But for comparison purposes in your program, it's easy enough to square the distance you're checking for and find out if your target is within range.

Login to post a reply

Server time is: 2024-11-25 06:30:36
Your offset time is: 2024-11-25 06:30:36