Sign in to follow this  

Need to rotate or flip? direction of objects and camera in game...

This topic is 2043 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok my idea is to rotate my avatar after it collides with an object in the game and I also need the camera to rotate 180 degrees. I at first thought just use a rotation matrix around the Y axis and walla... Not working I think it has to do with not being local to the model space?


I am assuming I can just rotate the position of the camera and the objects position?

Share this post


Link to post
Share on other sites
If I understand you correctly, then you want an orbiting of the camera around the colliders position. With [b]P[/b] and [b]O[/b] meaning the camera's current position and orientation, resp., and [b]C[/b] meaning the colliders position, orbiting with a rotation [b]R[/b] means to compute
[b]C[/b] * [b]R[/b] * [b]C[/b][sup]-1[/sup] * ( [b]P[/b] * [b]O[/b] )
here written down using column vectors. Because the camera is a bit apart from the collider, [b]C[/b] and [b]P[/b] are not the same and hence
[b]C[/b][sup]-1[/sup] * [b]P[/b] != [b]I[/b]
meaning that [b]R[/b] will change both the position as well as the orientation of the camera.

If you throw the same formula to the object directly, then [b]P[/b] is actually the same as [b]C[/b], so that (this time using [b]O[/b] for the orientation of the model)
[b]C[/b] * [b]R[/b] * [b]C[/b][sup]-1[/sup] * ( [b]C[/b] * [b]O[/b] ) = [b]C[/b] * [b]R[/b] * [b]O[/b]
will perform a model local rotation. Edited by haegarr

Share this post


Link to post
Share on other sites
Yeah, I am not 100% following this, but are these all vectors or positions? I am not doing any vector math on this.

Here is the camera code and my objects update()

[code]
//camera
void Update(void)
{
cml::matrix44f_c rot, trans, rotWorldY;
trans.identity();
rot.identity();
rotWorldY.identity();

cml::matrix_rotation_world_axis(rotWorldY, 1, cml::rad(180.0f));

cml::matrix_translation(trans, 0.0f, 0.0f, -radius);
cml::matrix_rotation_world_axis(rot, 0, cml::rad(xRot));
transformAvatar = trans * rot;

cml::matrix_translation(trans, -position[0], 0.0f, -position[2]);
cml::matrix_rotation_world_axis(rot, 1, cml::rad(yRot));
rotY = rot;
if(collision)
{
//transformAvatar *=rotWorldY;
rotY *=rotWorldY;
transform = transformAvatar * rot * rotWorldY * trans;
collision = false;
position = cml::transform_point(rotWorldY, position);
// position[0] += -2.0f;
// position[2] += -2.0f;
}
else
transform = transformAvatar * rot * trans;
}
inline void Move(float t)
{
float yRotRad = yRot / cml::constants<float>::deg_per_rad();
float xRotRad = xRot / cml::constants<float>::deg_per_rad();
position[0] += sinf(yRotRad) * t;
position[2] -= cosf(yRotRad) * t;
position[1] -= sinf(xRotRad) * t;
}
[/code]

[code]
//objects
void Player::Update(void)
{
NX::App* app = NX::App::Get();
position = app->GetCamera()->GetPosition();
transform = app->GetCamera()->GetAvatarPosition();
if(collision)
{
cml::matrix44f_c rot;
rot.identity();
cml::matrix_rotation_world_axis(rot, 1, cml::rad(180.0f));
transform *=rot;
collision = false;
}
UpdateAABB();
}
[/code] Edited by MARS_999

Share this post


Link to post
Share on other sites
[quote name='MARS_999' timestamp='1336925847' post='4939812']
Yeah, I am not 100% following this, but are these all vectors or positions? I am not doing any vector math on this.
[/quote]
All bold written capital letters in my post above are transformation matrices. E.g. [b]O[/b] is the rotational matrix that gives the camera its orientation in the world, and [b]P[/b] is the translational matrix that gives its position in the world. Because we use so-called homogeneous vector (those with mysterious 1 in the 4-th dimension) we are enabled to concatenate all usual transformation by multiplying the matrices together. That is what I've done in my post above.

When looking at the expression
[b]C[/b] * [b]R[/b] * [b]C[/b][sup]-1[/sup] * [b]P[/b] * [b]O[/b]
and remembering that I've used column vectors for explanation, the things that happen to the camera are these:
1. Rotate the camera so that it gets its world orientation: [b]O[/b]
2. Translate the camera so that it gets its world position *after* doing the rotation: [b]P[/b] * [b]O[/b]
(Notice that the position doesn't influence the orientation; you can see this when actually multiplying out the matrices on paper.)
3. Translate the camera by the [i]inverse[/i] (hence the [sup]-1[/sup] in [b]C[/b][sup]-1[/sup]) of the colliders position [b]C[/b], so [b]C[/b][sup]-1[/sup] * [b]P[/b] * [b]O[/b]
Of course, especially the [i]inverse of a translation[/i] is in fact a translation by the negative amount:
[b]C[/b](dx,dy,dz)[sup]-1[/sup] = [b]C[/b](-dx,-dy,-dz)
Hence we make a space where the collider is placed in the origin. The sense of this step is that [b]0[/b] (vector null) is ever part of a rotation axis, and we want to rotate around the collider's origin, don't we?
4. Then we do the "orbiting", in your special case a rotation by 180° around the y axis, denoted by [b]R[/b], so: [b]R[/b] * [b]C[/b][sup]-1[/sup] * [b]P[/b] * [b]O[/b]
5. At least undo step 3 so that the world isn't shifted any more: [b]C[/b] * [b]R[/b] * [b]C[/b][sup]-1[/sup] * [b]P[/b] * [b]O[/b] Edited by haegarr

Share this post


Link to post
Share on other sites
I will take a look at your explanation closer after a bit, but is the code I posted correct? and will your example work with what I am doing? I don't want to recode my code to do something else....

Share this post


Link to post
Share on other sites

This topic is 2043 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this