2 registered members (AndrewAMD, 7th_zorro),
1,285
guests, and 4
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
walking on walls -- alignToVec
#291799
09/28/09 03:55
09/28/09 03:55
|
Joined: Mar 2006
Posts: 3,538 WA, Australia
JibbSmart
OP
Expert
|
OP
Expert
Joined: Mar 2006
Posts: 3,538
WA, Australia
|
G'day! A while back when my old "quaternions" were getting a little more attention I had some requests for examples of how they could be used to rotate an object to align to a wall so that it can walk on whatever surface it wants, no matter what the angle is. I helped a couple of people with it, and have seen another request for such a function, so I thought people might appreciate it. This does not require my quaternion code. I personally prefer to use my quaternions (albeit a much more updated version than my previous contribution, which will get updated when the next public beta comes out), but this function here has been designed for use with normal A7 Euler angles (pan, tilt and roll). This is a (very slightly modified) function I wrote for Pappenheimer which other people might like:
function alignToVec(ANGLE* entAng, VECTOR* vec, VECTOR* axis, var factor) {
vec_rotate(axis, entAng);
vec_normalize(axis, 1);
vec_normalize(vec, 1);
VECTOR rotAxis;
vec_cross(rotAxis, vec, axis);
var angle = -acos((float)vec_dot(axis, vec)) * factor * vec_length(rotAxis);
ANGLE rotAngle;
ang_for_axis(rotAngle, rotAxis, angle);
ang_add(entAng, rotAngle);
}
entAng is the angle that gets modified. This will commonly be "my.pan", but if you're using c_rotate you won't use this function to modify "my.pan" directly. vec is the vector (in world space) that the angle should get lined-up with (this will commonly be "normal" after doing a c_trace or c_move). axis is the vector (in local space, relative to the angle) that needs to get aligned to "vec" (for walking on walls, this will commonly be "vector(0, 0, 1)", which is straight up relative to the angle). factor usually ranges from 0-1 (but there's nothing stopping you from using any other value), and indicates how far to rotate the angle (where 1 = complete alignment, and 0 = no rotation). TWO IMPORTANT NOTES:1. The alignment has been "smoothed out" to prevent stuttering/shaking due to var's finite precision, so calling "alignToVec" with a factor of "1" just once often won't be a complete alignment (it'll get most of the way there). Call "alignToVec(........, 1)" once every frame over several frames to complete the alignment (this will be quite smooth and quite quick), or several times in one frame for a complete alignment in only one frame. If you would rather deal with the imprecisions yourself, remove the "* vec_length(rotAxis);". If you call "alignToVec" with a factor of 1 every frame you will often come across a stuttering or shaking. 2. This requires the latest public beta (A7.8), but also relies on a bug in the latest beta. "ang_for_axis" currently uses radians instead of degrees. Apparently this has been fixed and will be solved in the next public release, and so this will require this:
-acos((float)vec_dot(axis, vec))
to be changed to this:
-acosv(vec_dot(axis, vec))
when the update comes out. I hope you find this useful! Jibb
Formerly known as JulzMighty. I made KarBOOM!
|
|
|
Re: walking on walls -- alignToVec
[Re: Saturnus]
#291811
09/28/09 08:12
09/28/09 08:12
|
Joined: Mar 2006
Posts: 3,538 WA, Australia
JibbSmart
OP
Expert
|
OP
Expert
Joined: Mar 2006
Posts: 3,538
WA, Australia
|
Cheers.
It could be solved by my quaternion code, but the main change I want to make to my quaternion code is that it uses ang_for_axis to convert back to a Euler angle (which is the reason 7.8 is needed here), instead of its current method (which isn't smooth when it approaches certain orientations). I would solve that by converting the quaternion to a matrix and rotating the entity with that, but the functions to modify an entity's orientation using matrices also require 7.8. Sorry.
Hopefully Lite-C will be updated with the next update. Feel free to give my quaternions a go anyway, as the conversion back to Euler angles should be precise enough for most situations (a few years ago I had physics that used my quaternion code and that went pretty smoothly).
Jibb
Formerly known as JulzMighty. I made KarBOOM!
|
|
|
Re: walking on walls -- alignToVec
[Re: JibbSmart]
#291912
09/28/09 21:38
09/28/09 21:38
|
Joined: Oct 2007
Posts: 306 Austria
Alan
Senior Member
|
Senior Member
Joined: Oct 2007
Posts: 306
Austria
|
Hi, very nice function and I would really like to use it, but I do not entirely understand what the "axis" parameter is supposed to be. Where do I obtain it from? ("vec" is clear, it's the normal of the surface, "entAng" is clear as well and "factor" is always 1 for my purpose) Could you please explain it to me once more, maybe going a little more into detail? Basically, all I want to do is having the feet of my model face the ground below them at any time, no matter what it looks like. I already asked that question in the other thread but I assume the chances are higher that you see it in here, besides it is part of this topic anyway
|
|
|
Re: walking on walls -- alignToVec
[Re: Alan]
#291921
09/28/09 22:07
09/28/09 22:07
|
Joined: Mar 2006
Posts: 3,538 WA, Australia
JibbSmart
OP
Expert
|
OP
Expert
Joined: Mar 2006
Posts: 3,538
WA, Australia
|
G'day I've already answered in your topic, but I'll answer here for the benefit of others. Basically, when you have an angle and a vector to line it up with, you need to decide how you'll line it up -- vec_to_angle, for example, lines up the angle's local x with the vector that's given to it. To do the same thing with my function, you'd pass it "vector(1, 0, 0)" -- except my function won't set roll to zero, as it makes the smallest rotation possible to line up the axis and the vector. For most situations (such as walking on walls) you want to line up the entity's "up" with the surface normal. "vector(0, 0, 1)" points straight up, so that's what you'll pass to the function, along with "normal". I hope I've made this clear now. Jibb
Formerly known as JulzMighty. I made KarBOOM!
|
|
|
Re: walking on walls -- alignToVec
[Re: JibbSmart]
#291926
09/28/09 23:07
09/28/09 23:07
|
Joined: Oct 2006
Posts: 175
Gumby22don
Member
|
Member
Joined: Oct 2006
Posts: 175
|
Nice contribution - when's the next Alien v Predator game being made in A7? Don have a great day
|
|
|
|