Only recently have I started messing about with the physics commands.
Anyway, I came across
phy ray cast all shapes. "Great", I thought, until I found it took vectors instead of angles.
Not a problem, just move the camera 1, take the position, move it back. Not the prettiest method, but it works, and its fast.
Anyway, I seem to have come across a few times (infact, hundreds recently) where manipulating the camera to return vectors does not work - and the program quits complaining I've not provided a unit vector.
So, 3 things.
First:
Anyone reading this, who is unsure what a unit vector is:
A unit vector is just a combined x/y/z distance equal to 1.0 exactly.
Second:
I wrote a little bit of error checking, to fix this. Its only 6 lines, but will handle the errors you might otherwise get.
root, xvec, yvec and zvec are all floats (VERY important).
root=sqrt((xvec*xvec)+(yvec*yvec)+(zvec*zvec))
if root<>1
xvec = (1/root)*xvec
yvec = (1/root)*yvec
zvec = (1/root)*zvec
endif
You can then adapt this to your own code just before you ray cast.
Third, and finally:
I got annoyed having to provide vectors, so to make it easier for myself (and others), here is the function I use to supply angles instead of vectors
function RayCastByAngle(xpos as float, ypos as float, zpos as float, xangle as float, yangle as float, zangle as float)
`Declare variables
root as float : unknown as float : tempobj as integer
xvec as float : yvec as float : zvec as float
tempxp as float : tempyp as float : tempzp as float
tempxa as float : tempya as float : tempza as float
` Store the camera position and rotation, so it can be returned later
tempxp=camera position x() : tempyp=camera position y() : tempzp=camera position z()
tempxa=camera angle x() : tempya=camera angle y() : tempza=camera angle z()
` move the camera into the right place
` This takes the x/y/z position and calculates the relative distance
` Then subtracts this value from the x/y/z position to get a value between -1 and 1
position camera xpos, ypos, zpos : rotate camera xangle, yangle, zangle
move camera 1
xvec=camera position x()-xpos
yvec=camera position y()-ypos
zvec=camera position z()-zpos
` Reposition and rerotate the camera back to where it was
position camera tempxp, tempyp, tempzp : rotate camera tempxa, tempya, tempza
` Ray Cast command needs a vector size of 1.0 exactly
` Sometimes this isn't achieved (by tiny amounts, often around 0.0005)
` So fix this problem to return a size of 1.0 exactly
root=sqrt((xvec*xvec)+(yvec*yvec)+(zvec*zvec))
if root<>1
xvec = (1/root)*xvec
yvec = (1/root)*yvec
zvec = (1/root)*zvec
endif
` Ray cast based on the supplied position and calculated vectors
` Not sure what it returns, so I called it unknown and set to float (just in case)
unknown=phy ray cast all objects(xpos, ypos, zpos, xvec, yvec, zvec)
` And return that value, just in case its needed elsewhere
endfunction unknown
It may need adapting to suit other situations, but thats what the community is for, right?
I don't really know what the return value of the ray cast command is, but I store and return it in case it is used elsewhere in a program.
I repositioned and rerotated the camera in this, since I couldn't get the newxvalue, newyvalue and newzvalue commands to return the right coordinate in 3D space first time around. If anyone else can, it might prove faster to use those than manipulating the camera.
And just so it doesn't mess up other programs, I move the camera back to where it was
EDIT 1: Fixed the ray cast call command, cheers codger