How to set tilt angle to the floor?

Posted By: Ayumi

How to set tilt angle to the floor? - 07/14/17 16:14

Hey,

I have been trying for days to adjust an tilt angle of a model to an angle of the ground.

That code is working, but the tilt is wrong. Any ideas?

Code:
vec_to_angle(my.tilt, vector(hit.nx, hit.ny, hit.nz));




EDIT: Fixed with ang_for_axis
Posted By: Superku

Re: How to set tilt angle to the floor? - 07/14/17 20:54

Your function argument "my.tilt" is not a vector. A(n acknex) vector consists of three consecutive var variables in the 1dimensional memory. When you write "my.tilt", the lite-C compiler sees that vec_to_angle required vector pointers, not a single var (which my.tilt normally is), so it adds the address operator for you automatically (pv = &(my.tilt) or &(my->tilt)).
The vec_to_angle function now calculates an orientation based on the direction vector/ its second argument and copies that into the vector pointer/ the first argument, meaning it overwrites three vars in a row: pv[0], pv[1], pv[2].
However, the address you wrote in the first argument was &(my.tilt), so my.tilt is overridden by pv[0], which normally is the pan value. Your desired tilt value is written into my.roll, and the bad part - should vec_to_angle change the roll value which I'm not sure/ I don't think it does, but other vec_... functions will - some random memory at address location pv[2] gets overriden.
This is really bad, and can and will lead to random behavior in your code/ game, random functions failing, game freezes/ crashes and so on. That's stuff that kills a project.

Always keep that in mind when functions require vectors/ vector pointers.
Either write
Code:
vec_to_angle(my.pan, hit.nx);


or
Code:
VECTOR temp; // or use an ANGLE, I always just use VECTORs
vec_to_angle(temp, hit.nx);
my.tilt = temp.y;


depending on what you want to achieve.
Posted By: Ayumi

Re: How to set tilt angle to the floor? - 07/15/17 09:32

Ty for information, seen to late my wrong angle.
Posted By: jumpman

Re: How to set tilt angle to the floor? - 07/18/17 16:53

awesome explanation superku! I unknowingly did this once recently, and random crashes in an unrelated function occurred.
© 2024 lite-C Forums