Jump to content

Getting random coords from LOWEST point of Cuboid


Geo

Recommended Posts

So I've the following Cuboid:
 

createColCuboid(-2846.07104, -1545.08484, 137.68427, 64.902099609375, 53.124267578125, 4.56259765625)


What I'd like to do (But have no idea on how to do it, or is it even possible) it's to make a function that gets a random coordinate from the LOWEST point of the cuboid. This is in order to create an object in a random position inside the cuboid. It has to be in the lowest point of the cuboid since the object shouldn't be floating in the air, but on top of the ground.

The cuboid data is stored in a cube named table, where its coords, width, depth and height are stored in. If it can't be done with the Cuboid object, maybe I can use this data to code the previous mentioned function?

EDIT = I'm guessing it involves some math, which is not my strong suit. If you know how to solve this, please don't mind sharing the mathematics fundamentals you used in order to solve this problem. Thank you so much!

 

Edited by Geo
Link to comment
8 hours ago, Geo said:

What I'd like to do (But have no idea on how to do it, or is it even possible) it's to make a function that gets a random coordinate from the LOWEST point of the cuboid. This is in order to create an object in a random position inside the cuboid. It has to be in the lowest point of the cuboid since the object shouldn't be floating in the air, but on top of the ground.

yes it is possible but the player needs to be kinda close to the area, like 600mts/units, for the collision data to load and processLineofSight or getGroundPosition to work, you can do a loop and wait for some player to be near and then trigger the function, I made a test code clientside, you can use it from far away and close to the area to see how it works:
 

--clientside

createColCuboid(-2846.07104, -1545.08484, 137.68427, 64.902099609375, 53.124267578125, 4.56259765625)

function cub()
math.randomseed(getTickCount())
    setTimer(function()
        local x=-2846.07104+math.random(64)--position random from x
        local y=-1545.08484+math.random(53)--position random from y
        local h, hx, hy, hz,_,_,_,_,_,_,_,model=processLineOfSight(x,y,150,x,y,130,_,_,_,_,_,_,_,_,_,true)--check for ground position and world model
        if not h then
            return outputChatBox("player too far")--if player is too far stop execution
        elseif model~=18306 then
            cub()--if randomizer hit the house or trees try again until hit the ground
            return
        end
        createObject(1238,hx,hy,hz+0.4)
        outputChatBox("obj created in: "..hx.." "..hy.." "..hz)
    end,100,1)--litle delay to randomizer
end
addCommandHandler("cuboid",cub)

you can adapt this concept to work serverside using triggers / elementdata / tables if needed

  • Thanks 1
Link to comment
33 minutes ago, _Ace said:

yes it is possible but the player needs to be kinda close to the area, like 600mts/units, for the collision data to load and processLineofSight or getGroundPosition to work, you can do a loop and wait for some player to be near and then trigger the function, I made a test code clientside, you can use it from far away and close to the area to see how it works:
 


--clientside

createColCuboid(-2846.07104, -1545.08484, 137.68427, 64.902099609375, 53.124267578125, 4.56259765625)

function cub()
math.randomseed(getTickCount())
    setTimer(function()
        local x=-2846.07104+math.random(64)--position random from x
        local y=-1545.08484+math.random(53)--position random from y
        local h, hx, hy, hz,_,_,_,_,_,_,_,model=processLineOfSight(x,y,150,x,y,130,_,_,_,_,_,_,_,_,_,true)--check for ground position and world model
        if not h then
            return outputChatBox("player too far")--if player is too far stop execution
        elseif model~=18306 then
            cub()--if randomizer hit the house or trees try again until hit the ground
            return
        end
        createObject(1238,hx,hy,hz+0.4)
        outputChatBox("obj created in: "..hx.." "..hy.." "..hz)
    end,100,1)--litle delay to randomizer
end
addCommandHandler("cuboid",cub)

you can adapt this concept to work serverside using triggers / elementdata / tables if needed



This is genius. I actually only needed to get the random X and Y from the colShape in order to determine the position for a new object.

 

function getElementObjectCoords()
    local zones = exports.zones:createZone()
    
    local width = zones[1]["width"]
    local depth = zones[1]["depth"]
    local X = zones[1]["coordsX"]
    local Y = zones[1]["coordsY"]
    local Z = zones[1]["coordsZ"]

    local ranX = X + math.random(width)
    local ranY = Y + math.random(depth)


    return ranX, ranY, Z
end

 

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