Techious
http://www.techious.com/forums/

Skillers... need to borrow your maths knowledge
http://www.techious.com/forums/viewtopic.php?f=16&t=7238
Page 1 of 1

Author:  azcn2503 [ Fri Feb 10, 2012 12:40 pm ]
Post subject:  Skillers... need to borrow your maths knowledge

Hi Skillers

Let's say I have a grid. At coordinates 122,87 there is a dot. At coordinates 198,178 there is the centre of a circle. The radius of the circle is 145.

How do I know if the position of the dot at 122,87 is within the area of the circle?

Pic:

Image

What I'm trying to do is return a true or false statement, is the red dot within the circle? Obviously this is for programming... so it would be like...

redDot.isWithinCircle(198,178,145) // return true/false

Many thanks in advance

Author:  DHR-107 [ Fri Feb 10, 2012 12:47 pm ]
Post subject:  Re: Skillers... need to borrow your maths knowledge

Well, if you know the location of the point, you can simply make a triangle and find the length of the hypotenuse... The hypotenuse being the side opposite the right angle.

Basically use Pythagoras. H = SQRT (O^2 + A^2) where H is Hypotenuse and A and O can be the other 2 sides.

In this instance... The difference from the center to the vertical line is 178 - 87 = 91 and 198 - 122 = 76. So therefore... making a right angled triangle with Sides 76 and 91 and putting them into the abvove formula = SQRT ( 76^2 + 91^2) = 118.56 (2dp). Then you just compare that against the radius... If it's less it's inside, if its more its outside...

Hope that works/helps :)

http://en.wikipedia.org/wiki/Pythagorean_theorem

I guess your function would basically be something like Checkpoint(pX, pY, CoC, R); Where CoC is the centre of the circle and pX and pY are the location of the point.

Also; I know I'm not Skillers :P

Might put up a pic lol

Image

Author:  azcn2503 [ Fri Feb 10, 2012 1:01 pm ]
Post subject:  Re: Skillers... need to borrow your maths knowledge

DHR-107 wrote:
Basically use Pythagoras. H = SQRT (O^2 + A^2) where H is Hypotenuse and A and O can be the other 2 sides.


Can't believe I didn't think of this. Not only does this work, but it's computationally trivial lol.

Thanks :D

Author:  DHR-107 [ Fri Feb 10, 2012 1:05 pm ]
Post subject:  Re: Skillers... need to borrow your maths knowledge

No problem :) Even went onto explain it all haha :P

Author:  azcn2503 [ Fri Feb 10, 2012 1:11 pm ]
Post subject:  Re: Skillers... need to borrow your maths knowledge

Code:
<script type="text/javascript"><!--

    function check(x,y,cx,cy,cr){
        var hyp=Math.sqrt(Math.pow(cx-x,2)+Math.pow(cy-y,2));
        return hyp<cr?true:false;
    }
   
    alert(check(122,87,198,178,145));

//--></script>


Returns true.

I need this code to work properly for the game I am making... if object is within certain distance then activate certain things, buffs, spells, bla bla etc. so thank you!

Author:  DHR-107 [ Fri Feb 10, 2012 1:12 pm ]
Post subject:  Re: Skillers... need to borrow your maths knowledge

Is that right or wrong? lol :/

On another note, semi murdering the forum page when that code block loads haha :P

Author:  azcn2503 [ Fri Feb 10, 2012 1:16 pm ]
Post subject:  Re: Skillers... need to borrow your maths knowledge

Hehe. Returning true is good :)

Author:  Skillers [ Fri Feb 10, 2012 5:39 pm ]
Post subject:  Re: Skillers... need to borrow your maths knowledge

Damn, here I was thinking this would be a difficult problem, not Pythagoras ><

Though as a quick note:

Code:
<script type="text/javascript"><!--

    function check(x,y,cx,cy,cr){
        var hyp2=(cx-x)*(cx-x)+(cy-y)*(cy-y);
        return hyp2<(cr*cr)?true:false;
    }
   
    alert(check(122,87,198,178,145));

//--></script>


Will, in general, be significantly quicker (though my JS syntax may be wrong). Computational square roots are generally pretty slow compared to multiplications, and the pow() function is usually a lot more complicated than needed for simple integer powers.

Author:  azcn2503 [ Sat Feb 11, 2012 2:12 am ]
Post subject:  Re: Skillers... need to borrow your maths knowledge

Thanks Skillers, that's definitely worth me considering when I'm working on this project.

Author:  Skillers [ Sat Feb 11, 2012 2:34 am ]
Post subject:  Re: Skillers... need to borrow your maths knowledge

No worries. TBH, it should be much of an issue, unless you are looking over that function a lot. It's something that's been drilled into us in our programming courses because in simulation like stuff, every little bit of performance tweaking can save you a lot of time on the runs.

Author:  azcn2503 [ Sat Feb 11, 2012 2:39 am ]
Post subject:  Re: Skillers... need to borrow your maths knowledge

This code will be used a lot so the cheaper it is the better!

Author:  Harry [ Sat Feb 11, 2012 5:47 pm ]
Post subject:  Re: Skillers... need to borrow your maths knowledge

Skillers wrote:
Damn, here I was thinking this would be a difficult problem, not Pythagoras ><

Though as a quick note:

Code:
<script type="text/javascript"><!--

    function check(x,y,cx,cy,cr){
        var hyp2=(cx-x)*(cx-x)+(cy-y)*(cy-y);
        return hyp2<(cr*cr)?true:false;
    }
   
    alert(check(122,87,198,178,145));

//--></script>


Will, in general, be significantly quicker (though my JS syntax may be wrong). Computational square roots are generally pretty slow compared to multiplications, and the pow() function is usually a lot more complicated than needed for simple integer powers.


Motherfucker beat me to it, I went along reading every post hoping nobody had put this :<

Author:  Skillers [ Sat Feb 11, 2012 10:22 pm ]
Post subject:  Re: Skillers... need to borrow your maths knowledge

Heh, spent far too much time optimising stuff like this not to :P

Author:  Harry [ Sun Feb 12, 2012 12:44 am ]
Post subject:  Re: Skillers... need to borrow your maths knowledge

I did it for downtown nazi mission, I think we know clearly who is the more of an intellectual being!

Author:  Skillers [ Sun Feb 12, 2012 1:50 am ]
Post subject:  Re: Skillers... need to borrow your maths knowledge

I did it for parallelised Monte Carlo simulations :P

Author:  Si [ Thu Feb 16, 2012 9:33 pm ]
Post subject:  Re: Skillers... need to borrow your maths knowledge

Aaron knowing what you would be using this for, it might be worth going even further and even requesting and storing the cr^2 value instead of the cr value. This will save you having to do a multiply every time this function is called and allow the CPU pipeline to schedule some other instruction you might need to do at the time :P

Ref: http://www.agner.org/optimize/instruction_tables.pdf (actually this document might of interest of anyone who has an interest in timings of computer architecture at a instruction set level)

Author:  azcn2503 [ Thu Feb 16, 2012 9:35 pm ]
Post subject:  Re: Skillers... need to borrow your maths knowledge

Thanks Si, this is even better.

Author:  Skillers [ Thu Feb 16, 2012 9:50 pm ]
Post subject:  Re: Skillers... need to borrow your maths knowledge

Heh, I assumed it was fairly common knowledge to store values repeatedly used ><

However, I also thought it wouldn't make a huge amount of difference with floating point multiplication, as those are really quick, but I think you're implying that architecture means it can be significant?

Author:  Harry [ Fri Feb 17, 2012 2:30 pm ]
Post subject:  Re: Skillers... need to borrow your maths knowledge

Regardless of how long it takes, if you're calling it every frame/movement or whatever, doing it once and storing it is better surely?

Author:  Skillers [ Fri Feb 17, 2012 4:23 pm ]
Post subject:  Re: Skillers... need to borrow your maths knowledge

Yes, and it is something that I do, just I would still think that the difference in this case would be insignificant. Just wondering if there's a reason why it helps more than I would think.

Author:  azcn2503 [ Fri Feb 17, 2012 4:28 pm ]
Post subject:  Re: Skillers... need to borrow your maths knowledge

Due to the dynamic nature of what I'm making, it's going to be difficult to know what to store...

But I think I have an idea for it.

The code might be used in conjunction with some sort of active entities object, that would remember this type of data. So for each frame of animation, it would just loop through something like entity.active[n].x and entity.active[n].y and the pre-set entity.active[n].r as r*2, maybe some other stuff... It's difficult though... things will be pushed and popped to the objects and arrays all the time. And whatever the client processes, the server processes tenfold, as it has visibility of everything.

It's difficult to describe in too much detail -- the Google doc has more information on how data will be sent/received, and goes in to some depth. It makes more sense in the doc.

Author:  azcn2503 [ Fri Feb 17, 2012 4:32 pm ]
Post subject:  Re: Skillers... need to borrow your maths knowledge

Skillers wrote:
Yes, and it is something that I do, just I would still think that the difference in this case would be insignificant. Just wondering if there's a reason why it helps more than I would think.


Basically... it's collision detection gone wild. Not only are these values processed in every frame of animation by the client, but for every frame of animation for every connected client on the server also. So it's important that the code is as efficient as possible so that the project can scale well with multiple users.

More detail... imagine a game...

3 players moving toward 2 enemies.
> all players have buffs. player 1 has a buff aura of 20m, player 2 has a buff auto of 10m, and player 3 has a buff aura of 30m.
> enemies have aggro radius of 10m
> player 1 moves
> player 1 movement data is sent to server
> server processes collision with other world entities, and also detects buff aura radius against other objects that can maintain buffs (are you too far away to maintain buffs on others? can others maintain their buffs on you? are you in an aggro radius? etc.)
> server movement and collision data sent back to other clients in same area
> player 2 moves
... etc

Author:  Skillers [ Fri Feb 17, 2012 5:28 pm ]
Post subject:  Re: Skillers... need to borrow your maths knowledge

I'm assuming you store all the buff radii somewhere, you could simply store the square with that aswell.

Also, do you need to update it every frame? Surely the movespeed would mean that you could do it every few frames and it would still feel responsive?

Author:  azcn2503 [ Fri Feb 17, 2012 6:38 pm ]
Post subject:  Re: Skillers... need to borrow your maths knowledge

It's possible I could do it every few frames instead of every frame.

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/