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.

AppGameKit Classic Chat / Get center of sphere?

Author
Message
Carharttguy
8
Years of Service
User Offline
Joined: 8th Jul 2016
Location: Belgium
Posted: 22nd Mar 2017 14:39
Hello

I want to get the center of a sphere object because I want to calculate if 2 meshes collide.
See this code:



Code is somewhat messy, it's the fault of my debugging :')
The formula for calculating the distance of 2 3D points should be correct, so I'm assuming I did something wrong with getting the center location of the sphere, altough, I cannot think what's wrong with it.
Any idea?

Thanks!
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 22nd Mar 2017 14:48 Edited at: 22nd Mar 2017 14:51
the center its the sphere position or not?

if u using float here u should use .0 because 2 will be integer
/ 2.0
AGK (Steam) V2017.02.28 : Windows 10 Pro 64 Bit : AMD (17.2.1) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
Carharttguy
8
Years of Service
User Offline
Joined: 8th Jul 2016
Location: Belgium
Posted: 22nd Mar 2017 14:59
Aha

The center of the sphere could be its position indeed.
But even then, the calculation is wrong in someway.



The distance is +- 11, radius is 16. The radius is certainly correct.
So the distance must still be wrong, but in the image you can see clearly that the sphere is further away.
Carharttguy
8
Years of Service
User Offline
Joined: 8th Jul 2016
Location: Belgium
Posted: 22nd Mar 2017 15:28
I just found the problem! I took the diameter of both spheres, I needed the radius!

Stupid!
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 23rd Mar 2017 07:50 Edited at: 23rd Mar 2017 07:50
You should change your distance calculator, the way you are doing it is slower than it could be.
This

Is exactly the same as this

But it is considerably faster to use multiplication, here's the proof:


Another method you can use to speed up the distance check (A LOT) is to square the result so that you can avoid square rooting the check.
Your distance check would be this

And then when checking if the distance is within a certain range you square the range, like this
AGK V2 user - Tier 1 (mostly)
Carharttguy
8
Years of Service
User Offline
Joined: 8th Jul 2016
Location: Belgium
Posted: 23rd Mar 2017 08:17
Hi Scraggle

I might be wrong, but I don't think *2 is the same as ^2

3*2 = 6
3^2 = 9

Or do I misunderstand you?
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 23rd Mar 2017 09:43
(ax-bx)^2.0 is (ax-bx)*(ax-bx)
AGK (Steam) V2017.02.28 : Windows 10 Pro 64 Bit : AMD (17.2.1) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 23rd Mar 2017 11:58
Sorry, my bad!
Markus' multiplication example is the way to go.
AGK V2 user - Tier 1 (mostly)

Login to post a reply

Server time is: 2024-11-24 18:39:55
Your offset time is: 2024-11-24 18:39:55