Jump to content

[HELP] Getting the closest point from massive


JAY.ANN

Recommended Posts

Posted (edited)

Hi. I'm kinda newbie in scripting and I need some help.

local points = {

{-2180, 293.3, 35.4},

{-2089, 84.2, 35.5},

{-2064, 83.7, 35.5},

}

How can I get the closest point to player from that massive? I'll be very grateful for any help.

Edited by J-N
Posted (edited)

You could divide that area into triangles and solve the closest-point problem for each rectangle, then take the point with the minimum distance out of all rectangles.

Edited by The_GTA
Posted
2 minutes ago, The_GTA said:

You could divide that area into triangles and solve the closest-point problem for each rectangle, then take the point with the minimum distance out of all rectangles.

My idea is to repeat vehicle spawn like it's realized in GTA Online. So there will be a lot of points 

Posted
Just now, J-N said:

My idea is to repeat vehicle spawn like it's realized in GTA Online. So there will be a lot of points 

Oh, those points are not supposed to span an area? I wonder why you mentioned a "massive" then. You want to get the point which is the closest of a fixed set of points, right? Then just do the following:

local function getClosestSpawnPoint(points, x, y, z)
    local closest_dist = false;
    local closest_x, closest_y, closest_z = false, false, false;
    
    for m,pt in ipairs(points) do
        local ptx, pty, ptz = pt[1], pt[2], pt[3];
        
        local dist = getDistanceBetweenPoints3D(x, y, z, ptx, pty, ptz);
        
        if not (closest_dist) or (closest_dist > dist) then
            closest_dist = dist;
            closest_x, closest_y, closest_z = ptx, pty, ptz;
        end
    end
    
    return closest_x, closest_y, closest_z;
end

...

-- The place you want to use the spawn point.
local player_x, player_y, player_z = getElementPosition(player);
local spawn_x, spawn_y, spawn_z = getClosestSpawnPoint(points, player_x, player_y, player_z);

-- TODO: use spawn_x, spawn_y, spawn_z

 

Posted
1 minute ago, The_GTA said:

Oh, those points are not supposed to span an area? I wonder why you mentioned a "massive" then. You want to get the point which is the closest of a fixed set of points, right? Then just do the following:

local function getClosestSpawnPoint(points, x, y, z)
    local closest_dist = false;
    local closest_x, closest_y, closest_z = false, false, false;
    
    for m,pt in ipairs(points) do
        local ptx, pty, ptz = pt[1], pt[2], pt[3];
        
        local dist = getDistanceBetweenPoints3D(x, y, z, ptx, pty, ptz);
        
        if not (closest_dist) or (closest_dist > dist) then
            closest_dist = dist;
            closest_x, closest_y, closest_z = ptx, pty, ptz;
        end
    end
    
    return closest_x, closest_y, closest_z;
end

...

-- The place you want to use the spawn point.
local player_x, player_y, player_z = getElementPosition(player);
local spawn_x, spawn_y, spawn_z = getClosestSpawnPoint(points, player_x, player_y, player_z);

-- TODO: use spawn_x, spawn_y, spawn_z

 

Yup, you got me right! Soon I'll answer if my problem was solved or not

Solved. Thank you <3

Posted
46 minutes ago, J-N said:

Solved. Thank you ❤️

You're welcome!

Hoping to see you back here asking more questions. I will be there to help you again.

Posted
5 hours ago, The_GTA said:

You're welcome!

Hoping to see you back here asking more questions. I will be there to help you again.

I'm not sure that I abled to ask another question on another topic here, so therefore, I apologize in advance.

Everything works fine, thank you. But now I need some help in making an "appearence animation" for my car by using setElementAlpha().

I'd like to use InterpolateBetween() but I don't have any experience of working with it.

Posted
6 hours ago, J-N said:

... But now I need some help in making an "appearence animation" for my car by using setElementAlpha().
I'd like to use InterpolateBetween() but I don't have any experience of working with it.

I recommend you to use interpolateBetween using the "OutInQuad" easing function. It expects a value between 0..1 inclusive as progress parameter. This can be a time parameter if you divide elapsed time by a duration. If the time has elapsed completely then you should not use interpolateBetween anymore but instead treat the vehicle as completely visible.

How about you try your hands at it? If you know the above modular details then it is not a matter of experience but mathematical profoundness.

Posted (edited)
2 hours ago, The_GTA said:

I recommend you to use interpolateBetween using the "OutInQuad" easing function. It expects a value between 0..1 inclusive as progress parameter. This can be a time parameter if you divide elapsed time by a duration. If the time has elapsed completely then you should not use interpolateBetween anymore but instead treat the vehicle as completely visible.

How about you try your hands at it? If you know the above modular details then it is not a matter of experience but mathematical profoundness.

Well, I've been trying to do something with it in two or three different ways by using examples from mta.wiki but I didn't make it, bruh. Yup, I guess I'm too dumb in math

Edited by J-N
Posted
Just now, J-N said:

Well, I've been trying to do something with it in two or three different ways by using examples from mta.wiki but I didn't make it, bruh

Could you show me the code?

Posted
Just now, J-N said:

Well, I've been working with my script and now it works fine.

Solved. Again. Xd

Thanks a lot for your help!

Glad to be of help to you! I admit that I wanted to get you to look at your own code instead of me solving it outright for you. In my opinion a really good way to learn is to self-reflect based on your own accomplishments.

If you would have posted the code I could have given my solution of course. But this is not required anymore since you have solved the issue.

  • Like 1

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...