Author Topic: get location between objects/points?  (Read 430 times)

Baaleos

  • Administrator
  • Hero Member
  • *****
  • Posts: 1916
  • Karma: +0/-0
get location between objects/points?
« on: January 30, 2013, 05:14:11 pm »


               Would anyone happen to have a function or set of functions handy that can
Calculate the center position between 2,3,4,5 objects?
Eg
location l = GetCenterBetween(object1,object2);
location l = GetCenterBetween(object1,object2,object3);
location l = GetCenterBetween(object1,object2,object3,object4);
location l = GetCenterBetween(object1,object2,object3,object4,object5);

I'd like to be able to let my players drop items on the ground, in a circle shape, and the system be able to calculate the center position between the objects - and try to spawn a portal,
or summon a creature between them.
I can see that it should be doable, but im just not familiar with vector functionality that much.
I imagine it being like this
Player drops items in an arrangement like this

             o
       o
             x     o
 
        o        o
Even though its not a perfect shape, it still logically has a center. A place that is bisected by all points?
x being the point I want to get, based on the locations of the objects (o) around it.
               
               

               
            

Legacy_FunkySwerve

  • Hero Member
  • *****
  • Posts: 2325
  • Karma: +0/-0
get location between objects/points?
« Reply #1 on: January 30, 2013, 05:42:53 pm »


               We have a vector library floating around (>< @ pun). I don't think you need it though. Just GetPositionFromLocation of their location, and average all the x values, then all the y values. Simple example: 10, 10  10,-10, -10, -10, -10, 10 defines a square centered on 0,0 with sides 20 long. This approach gives you 0,0 as the center. Should work, though it doesn't guarantee that the shapes form a shape without crossed lines...

Funky
               
               

               


                     Modifié par FunkySwerve, 30 janvier 2013 - 05:43 .
                     
                  


            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
get location between objects/points?
« Reply #2 on: January 30, 2013, 06:33:04 pm »


               I agree with funcky,   You should be able to do it without spliting up the vectors.  Just add then together then devide.  
 vector GetCenterBetween(object o1,object o2,object o3,object o4,object o5)
{
   return  (GetPosition(o1) + GetPosition(o2)+ GetPosition(o3)+ GetPosition(o4)+ GetPosition(o5))/5.0;

}
               
               

               
            

Baaleos

  • Administrator
  • Hero Member
  • *****
  • Posts: 1916
  • Karma: +0/-0
get location between objects/points?
« Reply #3 on: January 30, 2013, 10:34:24 pm »


               Ah- cool.
I didn't realise it would be as simple as that.
I know that different shapes could potentially yield slightly off centre results.
But if it looks like its in the middle or at least between several of the points- it will have achieved the desired result.