Topic: Having a ray perpendicular to the ground

Hey there!
So I'm just messing around with this engine and I'm feeling pretty comfortable with it. And lately I have played with some raycasts. There's one thing that I am really not able to do. I've got my object. It's floating. Works. But now the point is, that I want the object to always be parallel to the ground. So I thought, I could do this by shooting a ray into the ground and check if that is perpendicular. But I can't get this working. Maybe you could help me.

In case you are wondering what I am trying to do, go drive that ramp up. Controls are W A S D, have fun! http://www.file-upload.net/download-684 … t.zip.html

Edit: Im kinda unexperienced in coding and this helps me getting used to it a lot. I would prefer ideas of how to do it over real code snippets. Thanks in advance smile

Last edited by ChickenRunFast (2012-11-23 16:09:39)

Re: Having a ray perpendicular to the ground

Hi,

an idea, use an invisible small cube placed in front of the floating object.
Make the floating obj looking at the small cube (using the look-at behavior).
Send a ray from the transparent cube and make it float at the same height, and it should be parallell.

Re: Having a ray perpendicular to the ground

Alright, the idea is good I guess, thanks for the reply.
But there's something I have to do about the definition of my floating. Because currently it is set to 10m above the ground (=10m on the z-axis) which obviously is not right. So I have to redefine my ray. I am aware of trigonometry, but I can't get this working. I have to get the ray rotating with the object, but I don't know why. Any idea? smile

Re: Having a ray perpendicular to the ground

We need to add some ready made functions to manipulate vectors,
but for now you can do it in lua with something like that :

function rotateX(vec, angle)

    zAngle = math.rad(angle)
    sinZ = math.sin(zAngle)
    cosZ = math.cos(zAngle)

    return {vec[0], vec[1] * cosZ - vec[2] * sinZ, vec[1] * sinZ + vec[2] * cosZ}

end

function rotateY(vec, angle)

    zAngle = math.rad(angle)
    sinZ = math.sin(zAngle)
    cosZ = math.cos(zAngle)

    return {vec[0] * cosZ + vec[2] * sinZ, vec[1], -vec[0] * sinZ + vec[2] * cosZ}

end

function rotateZ(vec, angle)

    zAngle = math.rad(angle)
    sinZ = math.sin(zAngle)
    cosZ = math.cos(zAngle)

    return {vec[0] * cosZ - vec[1] * sinZ, vec[0] * sinZ + vec[1] * cosZ, vec[2]}

end