Topic: Rayhit

Hello! Could someone please explain to me how to use Rayhit?

   function onSceneUpdate()
       start = {0, 0, 0}
       end = {100, 0, 0}
       point = rayHit(start, end, object)
       if point then
           print(point)
       end
   end

I understand that it goes from the start to the end and if it intersects with an object it prints out "point" in the console. What I don't understand is from where the ray starts from.

Re: Rayhit

It's like a 3d line, it starts from the start position and goes till end position.
In this exemple it goes from xyz(0, 0, 0) to xyz(100, 0, 0).

If you want to use it to detect a weapon shot, you set start as the tip of the gun and end as the maximum point the bullet can go.
The return point will be what you touched.

I think I'll make a real example soon.

Re: Rayhit

Nice, now I understand it more clearly.

guntip = getObject("guntip")

    Apos = getPosition(guntip)
       
    A = {Apos[1], Apos[2], Apos[3]}
        B = {100, 0, 0}
   
    if onKeyDown("MOUSE_BUTTON1") then
        point = rayHit(A, B)
        if point then
        print(point)   
        end
    end

This is what I came up with, is it used correctly?

Re: Rayhit

I guess I have written a little too much on the forums lately and asking abit too much. My game is coming along really great, and now the only thing that's needed is a combat system. I have recently started to build an AI-system which makes enemys go somewhat randomly in the world. The only thing that's needed now is that I want to know if I have used the script up above correctly or if something is needed to make it work correctly.

Re: Rayhit

B is not correct, you have to give the final point of the ray.

With the new release I'll show a simpler way to do it with the matrix operation, but for now it can be done like that :

http://www.maratis3d.org/wp-content/uploads/2013/01/rayHit.jpg

create 2 boxes A and B, don't link it to any objects (because getPosition return the local position if parented)
instead use the "Follow" behavior in "local" mode and set the offset to have A and B like the image (A for the tip and B to give the ray direction).

then in script :

Apos = getPosition(A)
Bpos = getPosition(B)
        
-- set Bpos further (1000 is the maximum distance, set higher if needed)
Bpos[1] = Apos[1] + (Bpos[1] - Apos[1])*1000
Bpos[2] = Apos[2] + (Bpos[2] - Apos[2])*1000
Bpos[3] = Apos[3] + (Bpos[3] - Apos[3])*1000

if onKeyDown("MOUSE_BUTTON1") then 
    point = rayHit(Apos, Bpos)
    if point then
        print(point)    
    end
end

Re: Rayhit

You have to understand that rayHit is like a laser detector in a spy film :

http://blogs.lt.vt.edu/blorg/files/2012/04/GetSmartLaser1-300x225.jpg

You have to give it the first point and the last point, and if something touch it it detect the intersection.

Re: Rayhit

This is great, thank you. It works perfectly!

Re: Rayhit

I have a question. Does:

point = rayHit(Apos, Bpos)

have to be in the if statement? Could I do:

Apos = getPosition(A)
Bpos = getPosition(B)
point = rayHit(Apos, Bpos)
        
-- set Bpos further (1000 is the maximum distance, set higher if needed)
Bpos[1] = Apos[1] + (Bpos[1] - Apos[1])*1000
Bpos[2] = Apos[2] + (Bpos[2] - Apos[2])*1000
Bpos[3] = Apos[3] + (Bpos[3] - Apos[3])*1000

if onKeyDown("MOUSE_BUTTON1") then 
    if point then
        print(point)    
    end
end

And be fine?

Re: Rayhit

If you don't put it in the if statement, the rayHit will be performed at every sceneUpdate (60 times per second!).
If you put it inside, it will be performed only when you click.

It depends on your needs: do you need to perform it all the time or at demand? The choice is yours. big_smile

Re: Rayhit

com3D wrote:

If you don't put it in the if statement, the rayHit will be performed at every sceneUpdate (60 times per second!).
If you put it inside, it will be performed only when you click.

It depends on your needs: do you need to perform it all the time or at demand? The choice is yours. big_smile

Ahh! I see. So the point variable only exists if the mouse is clicked. Otherwise it doesn't? Gotcha!