Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Help with collision on my fake physics script?

Asked by 8 years ago

Yay, more on my fake physics because it'd be easier for what I'm making so, errors on collision, it only works on certain studs, could someone help?

This script detects the collision, for my local to handle, and say whether something's in the way or not :

detectLeft=function()
    local ray = Ray.new(script.Parent.Parent.CFrame.p,((CFrame.new(100,0,0) * CFrame.new(script.Parent.Parent.CFrame.p)).lookVector).unit * 1) 
    local ignore = script.Parent
    local hit, position, normal = workspace:FindPartOnRay(ray, ignore)
    if hit then
        script.Parent.Left.Value=true
    else
        script.Parent.Left.Value=false
    end
end

detectRight=function()
    local ray = Ray.new(script.Parent.Parent.CFrame.p,((CFrame.new(-100,0,0) * CFrame.new(script.Parent.Parent.CFrame.p)).lookVector).unit * 1) 
    local ignore = script.Parent
    local hit, position, normal = workspace:FindPartOnRay(ray, ignore)
    if hit then
        script.Parent.Right.Value=true
    else
        script.Parent.Right.Value=false
    end 
end

detectForward=function()
    local ray = Ray.new(script.Parent.Parent.CFrame.p,((CFrame.new(0,0,100)  * CFrame.new(script.Parent.Parent.CFrame.p)).lookVector).unit * 1) 
    local ignore = script.Parent
    local hit, position, normal = workspace:FindPartOnRay(ray, ignore)
    if hit then
        script.Parent.Forward.Value=true
    else
        script.Parent.Forward.Value=false
    end 
end

detectBackwards=function()
    local ray = Ray.new(script.Parent.Parent.CFrame.p,((CFrame.new(0,0,-100) * CFrame.new(script.Parent.Parent.CFrame.p)).lookVector).unit * 1) 
    local ignore = script.Parent
    local hit, position, normal = workspace:FindPartOnRay(ray, ignore)
    if hit then
        script.Parent.Backwards.Value=true
    else
        script.Parent.Backwards.Value=false
    end 
end

game:service'RunService'.Stepped:connect(function()
    detectLeft()
    detectRight()
    detectForward()
    detectBackwards()
end)

Is it that I'm not using lookvector, on the raycasting, or what? I'll experiment while waiting for answers. If you find nothing wrong, and you don't know what the problem is, it's that the collision only works in certain directions.

1 answer

Log in to vote
0
Answered by
Legojoker 345 Moderation Voter
8 years ago

Funny that I ran into this; I have an artificial collision script as well, but because collisions are much more complex than simply a few vectors (since objects can interfere with other objects from multiple angles), I simply use the "theoretical cylinder" method. Objects are checked based on their magnitude to others on the X,Z plane, and then a predetermined height based on the object being checked is compared to all other objects to make sure the new position won't collide into others on this axis as well. However, this is because all parts in this game are anchored to help deny lag. Here's a visual of how my method works:

http://prntscr.com/8njm5m

Since each of these models only have to calculate based on a rough cylinder occupancy space, the server doesn't have to do as much math and the result is still reasonable. The main problem with your code is that it can only detect objects along the four directions it's facing. For example,

http://prntscr.com/8njj3u

in this picture, block 2 is avoiding the four rays coming off of block one's collision script. Even block 2's collision script does not notice the impending collision because of the positioning of the rays.

Unfortunately, this is inevitable for ANY number of rays off of a block, since there will always be an object small enough heading in just the right angle to cause a collision, even though if there are more rays (per angle measurement), only the less severe collision errors can occur. I would suggest using the cylinder method, or even a sphere method shown below (where you just compare magnitudes, but based on all 3 axes instead of 2).

http://prntscr.com/8njng2

If your objects are specifically checking for square collision properties, I'm not sure there is a full-proof method for this, at least in my eyes, without just letting ROBLOX's unanchored physics engine handle it (even though it can be glitchy). I hope I helped.

0
Ah, thanks, :) Quil_Cyndaquil 26 — 8y
0
Make sure to select my answer if this helped, it gives us both rep! Legojoker 345 — 8y
Ad

Answer this question