Jump to content

Help with Collision calculator


Recommended Posts

TL;DR, this script calculates two objects and their trajectory and checks if they intersect in any given point, however, it just works sometimes... and sometimes it doesnt.

As you can see, the code DOES work, since it creates a marker where it is expected to hit the target (Where the collision warning was triggered), however, i'm able sometimes to just hit the target with my missiles flawlessly and the warning wont trigger.

http://i.imgur.com/ha7hS6r.jpg

EDIT: Some more images here and here .

Script:

  
  
local pos = localPlayer.position 
local veh = createVehicle(411, pos.x + 10, pos.y, pos.z) 
setElementFrozen(veh,true) 
function calculateTrajectory(Object1,Object2,time) 
local p = {} 
p[1] = Vector3(Object1.position) 
p[2] = Vector3(Object2.position) 
  
local v = {} 
v[1] = (Vector3(Object1.velocity) * 50) 
v[2] = (Vector3(Object2.velocity) * 50) 
  
for i=1, math.floor(time/50) do 
local time = time +  i * 50 
local nP = {} 
nP[1] = p[1] + ((v[1]) * (time/1000)) 
nP[2] = p[2] + ((v[2]) * (time/1000)) 
  
d = nP[1] - nP[2] 
if (math.abs(d.x) > 20) or (math.abs(d.y) > 20) or (math.abs(d.z) > 20) then 
else 
outputChatBox(math.abs(d.x) .. " " .. math.abs(d.y).. " "..math.abs(d.z)) 
end 
    if math.abs(d.x) < 5 and math.abs(d.y) < 5 and math.abs(d.z) < 5 then 
    outputChatBox("Collision warning!") 
    createMarker(nP[1],"corona",4,255,0,0) 
    return 
    end 
end 
end 
  
addEventHandler("onClientProjectileCreation",root,function() 
calculateTrajectory(source,veh,getProjectileCounter(source)) 
outputChatBox('projectile',255,0,0) 
end) 
  

PD: i know there should be a correct formula for calculating this, bear with my lazyness to make it with a loop.

Link to comment
function collision (projectile) 
    local time = projectile.counter / 10 
    local velocity = projectile.velocity 
    velocity = velocity * time 
    -- 
    local position = projectile.position + velocity 
    local sight, x, y, z = processLineOfSight( 
        projectile.position, 
        position, 
        true, 
        true, 
        true, 
        true, 
        true, 
        true, 
        true, 
        false, 
        nil, 
        true     
    ) 
    position = (sight and Vector3(x, y, z)) or position 
    -- 
    Marker( 
        position, 
        'corona', 
        3 
    ) 
end 
  
addEventHandler( 
    'onClientProjectileCreation', 
    root, 
    function() 
        collision(source) 
    end 
) 
  

Link to comment
function collision (projectile) 
    local time = projectile.counter / 10 
    local velocity = projectile.velocity 
    velocity = velocity * time 
    -- 
    local position = projectile.position + velocity 
    local sight, x, y, z = processLineOfSight( 
        projectile.position, 
        position, 
        true, 
        true, 
        true, 
        true, 
        true, 
        true, 
        true, 
        false, 
        nil, 
        true     
    ) 
    position = (sight and Vector3(x, y, z)) or position 
    -- 
    Marker( 
        position, 
        'corona', 
        3 
    ) 
end 
  
addEventHandler( 
    'onClientProjectileCreation', 
    root, 
    function() 
        collision(source) 
    end 
) 
  

While it probably works, it doesnt apply to my case. It checks if anything is in the missile path, but if an object is moving and enters the path after it was fired, it wouldnt work. I.e. It doesnt take into consideration the target's movement.

Link to comment

It indeed does not check if given projectile has a target, but still does work with static elements. Now, you might instead do a per-frame check and find out whether an element interferes with the projectile, or through loop as in your code. I would do some sort of sight check around the projectile and check if there is any nearby approaching element that might end up colliding given its velocity within a certain distance.

Also, I had a few issues calculating what the final position is going to be and dividing by 10 worked just fine there for testing, though you might want to replace this with proper calculations.

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