Jump to content

Quick question about getElementsWithinColShape.


Oscuro87

Recommended Posts

Hi! :)

Just a quick question regarding getElementsWithinColShape:

Does the table that results from this function have a particular order?

Example : Assume there are at least 2 elements detected with the function getElementsWithinColShape(x,y), also assume that the colShape is a sphere, and is attached to the player.

Would the resulting table's first element be the one that is the nearest to the player?

Or would the first element of the table be the furthest element from the player?

Or even, would the table's elements not have any particular order? (random?)

Thanks in advance! :) (And don't hesitate to ask for precision, my English is far for quality!)

Link to comment

Sorry for the late reply, i was off for a couple of days!

Yes i know it returns an indexed table indeed, my question was more like :

"Does this indexed table has a particular order?

Like, is the nearest objects from the center of the colsphere at index 1 in the table?

Or has the table a random order?"

Sorry i wasn't clear! D:

Thanks though! :)

Link to comment
Guest Guest4401
Sorry for the late reply, i was off for a couple of days!

Yes i know it returns an indexed table indeed, my question was more like :

"Does this indexed table has a particular order?

Like, is the nearest objects from the center of the colsphere at index 1 in the table?

Or has the table a random order?"

Sorry i wasn't clear! D:

Thanks though! :)

No, they aren't arranged in an order depending upon distance. However, it's easy enough to sort them in that way.

Example:

local colShape = createColSphere(0,10,20,50) -- 0,10,20 are x,y,z position, 50 is radius 
local elements = getElementsWithinColShape(colShape) -- get elements 
  
table.sort(elements,  
    function(a,b) 
        local x, y, z = getElementPosition(colShape) 
        local x1, y1, z1 = getElementPosition(a) 
        local x2, y2, z2 = getElementPosition(b) 
        return getDistanceBetweenPoints3D(x,y,z,x1,y1,z1) < getDistanceBetweenPoints3D(x,y,z,x2,y2,z2) 
    end 
) -- the table is now sorted, elements[1] is the nearest element whereas elements[#elements] is the farthest. 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...