How do I rotate glm camera - c++

I have been working for a couple of weeks to get my glm camera to rotate but to no avail. I have try quite a few different methods. The first one is best camera i've had. If someone could explain to me where i'm going wrong, and how to fix it, THANK YOU!
1 - Problem - If i look, over my shoulder, aka 90 degrees to the left and try to look up it won't.
void addRotation(const glm::vec3 rot) {
_for = glm::rotate(_for, rot.x, glm::vec3(0,1,0));
_for = glm::rotate(_for, rot.y, glm::vec3(1,0,0));
}
_for is a vec3 in the direction i'm looking.
glm::mat4 getViewProj() { return proj * lookAt(_pos, _pos + _for, _up); }
proj is defined as
proj = glm::perspective(fov, aspect, 0.01f, 100.0f);
This code didn't really work but i'll include it anyway.
void addRotation(const glm::vec3 rot) {
glm::mat4 rx = glm::rotate(glm::mat4(1.0f), rot.x, glm::vec3(0,1,0));
glm::mat4 ry = glm::rotate(glm::mat4(1.0f), rot.y, glm::vec3(1,0,0));
_for = rx * ry; //_for was a mat4, everything else was the same
}
If have tried += on the first method, but all I is math errors because the decimal point goes basicly to infinity (though if i add a constant number that end in 0 it would fix that).
If anyone can help it would be MUCH appreciated. Thank you in advance!

Ok, I have found a solution after 4 more hours of looking,
Issue with GLM Camera X,Y Rotation introducing Z Rotation
This is a link to another glm camera issue question. Their problem was that there camera was doing random barroll rolls, while mine didn't pitch when looking to my left. But after copied down their code the solution I found to it was everywhere but when you ASSIGN the new up vector in there code, replace it with glm::vec3(0.0f, 1.0f, 0.0f). I did this with a variable and it has work fine for me no bugs yet.
I'm not fully sure I understand why this answer worked myself, but from what I'm guessing is happening when assigning a variable using a variable that's been adjusted already it's going to slowly create math errors. But having a concrete reference point and just using the adjusted up variable in the glm::lookAt calculation works fine. But if anyone can explain it better, knows a better solution, or knows for sure why it works please do tell me.
P.S. I am not deleting this because anyone else, who was in the situation I was in, will hopefully find this comment useful.

Related

How to add camera damping?

I asked a question about how to add camera damping in Ogre but didnt get any answer so here is a more vague question.
How would you add camera damping?
I googled this question and got answers in XNA and Unity and each is different than the other so I cant even figure out what technique, function or maths they are using.
I have a camera and its position, I have an object and the position where I want the camera to be and slowly move it to that position, how can I do this?
I tried using lerp but it didnt work, I dont know if that is the wrong way of doing it or my lerp function might be wrong so I dont know.
Can someone please help me out. Thanks
Here is my lerp function
Ogre::Vector3 lerp (Ogre::Vector3 &sourceLocation, Ogre::Vector3 &destLocation, Ogre::Real Time)
{
return sourceLocation+ (destLocation - sourceLocation) * Time;
}
in cpp file
this->camPos = this->lerp(this->camPos, this->playerNode->getSceneNode()->getPosition() + Ogre::Vector3(0,60,-100), 1000.0f);
this->getCamera()->setPosition(this->camPos);
but the camera just ends up miles away from the object
Thanks for answering Peter. Makes a bit more sense now, the lerp function is just returning a long vector since the time is constant however Im not sure about the second part.
I need to have a variable that increments with the frame?
Ogre::Real frametime += frame_event.timeSinceLastFrame * 0.01;
this->camPos = this->lerp(this->camPos, this->playerNode->getSceneNode()->getPosition() + Ogre::Vector3(0,60,-100), frametime);
this does slowly move the camera towards the target and then stop but since the frametime is increasing, the time it takes to get to the target destination gets quicker as well. do I just set the frametime to 0 when it reaches destination?
can you please explain a bit more about the second part. I would really really appreciate your help.
Thanks
Your calculation for lerp is the issue, your getting the vector between dest and source and massively scaling it up.
Your lerp time should not be constant, it should be scaling from 0 to 1 based on the time period you want to go from source to dest.
Before moving:
float length= (dest -start).Length();
Update ()
float distancedTravelled = (CurrentTime - StartTime) * cameraSpeed;
float lerp = distanceTravelled /length;
Pass lerp to function.
Faster camera speed is the quicker you go

DirectX Camera positioning (Third Person View)

I am creating a 3d flying game and using DXUTCamera for my view.
I can get the camera to take on the characters position, But I would like to view my character in the 3rd person.
Here is my core for first person view
//Put the camera on the object.
D3DXVECTOR3 viewerPos;
D3DXVECTOR3 lookAtThis;
D3DXVECTOR3 up ( 5.0f, 1.0f, 0.0f );
D3DXVECTOR3 newUp;
D3DXMATRIX matView;
//Set the viewer's position to the position of the thing.
viewerPos.x = character->x; viewerPos.y = character->y;
viewerPos.z = character->z;
// Create a new vector for the direction for the viewer to look
character->setUpWorldMatrix();
D3DXVECTOR3 newDir, lookAtPoint;
D3DXVec3TransformCoord(&newDir, &character->initVecDir,
&character->matAllRotations);
// set lookatpoint
D3DXVec3Normalize(&lookAtPoint, &newDir);
lookAtPoint.x += viewerPos.x;
lookAtPoint.y += viewerPos.y;
lookAtPoint.z += viewerPos.z;
g_Camera.SetViewParams(&viewerPos, &lookAtPoint);
So does anyone have an ideas how I can move the camera to the third person view? preferably timed so there is a smooth action in the camera movement. (I'm hoping I can just edit this code instead of bringing in another camera class)
Well, I believe I can help you in theory to change from a first person view to a third person. I'm sorry I can't give you the actual code, but I'm typing from a phone. You will have to put the point where your view starts slighty behind the player and make the lookAtPoint to look at the player. Also, be sure to make the , x , y and z to move according to the third person's logic. Hope that helps. I'm sorry if it doesn't help at all, but typing from a phone is kinda hard for me, and I can't explain it really good.

Inverse Interpolation with angles greater than Pi?

I'm making an API for skeletal animation. Right now it works fine, except Lets say you want to go from 2.0f to 1.0f. It will end up doing almost a full circle when it should only do about 1/6th of one.
I think I've got a way to find it it should go counter clockwise but I'm not sure how to use it with this:
bool CCW = fmod( (endKeyFrame->getAngle() -
startKeyFrame->getAngle() + TWO_PI), TWO_PI) > 3.141592;
remainingInterpolationFrames = endKeyFrame->getFrame() - startKeyFrame->getFrame();
//Linear interpolation
curIncreaseAngle = (endKeyFrame->getAngle() -
startKeyFrame->getAngle()) / remainingInterpolationFrames;
Thanks
I think this may help. Especially sections 8,9 and 30.

How can I make the camera fly through objects in OpenGL using Glut?

First off, all of my code can be found here, it's in 3 files.
flythrough.cpp, support.cpp, support.h
Okay... so if you run that, it seems to work, but a few things are really bugging me!
The camera is starting off in a strange location, I try to change all kinds of variables, but it doesn't seem to have an effect.
The biggest issue right now is moving the camera. At this point in time... the camera WILL move left and right, but NOT forward or backward. When I try to move forward, it's like the pixels are doing the correct thing, the object will eventually vanish as I move forward, like I've moved past it, but the object itself is not "appearing larger" as I get closer. Same for moving backward.
Please let me know if you can come up with anything.
I'd like to add that a majority of this code comes from my textbook. With modifications from myself.
Thanks!
Its an orthographic camera, therefore you cannot move along the Z axis. You need to change your camera type to do what your trying to do.
Ref: Line 33, flythrough.cpp
glOrtho(-2.0 * 64 / 48.0, 2.0 * 64 / 48.0, -2.0, 2.0, 100, 100.0);
Typically, just uncomment this line, then write the following:
gluPerspective(60.0, 640.0 / 480.0, 0.1, 1000.0);
This will multiply a perspective matrix by the currently loaded identity matrix (LoadIdentity()).
Ref: http://www.opengl.org/sdk/docs/man/xhtml/gluPerspective.xml
Without trying to work out your axis, have you tried moving the camera back and forwards along the Z axis instead of Y. Perhaps your depth axis is setup to be Z not Y.

How can I find the angle between two points on a bezier curve?

In my current OpenGL project I am trying to make the links of a chain hug the contours of a Bezier curve. How can I find the angle between two points on the curve so that I can position the links of the chain so that they follow the curve.
Here is a picture of the curve and chain, I need some way of rotating the links so that they follow the curve.
Does anybody here know how to do this?
Maybe something like this is what you need.
How to calculate the tangent to a Bezier curve
This is hard to find online. It must be a secret closely held by those who know. Oh you can find the math formulae, but have fun interpreting them if you are not a mathematician. So what is a poor developer to do? Go back to school.
I spent a couple days bashing my skull over this one. I googled my brains out (which was easier once my skull was sufficiently bashed). Then one bright beautiful Saturday, I was holed up in my developer's dungeon resting my weary bones. I had the TV on in front of me and Wikipedia to the right and there I was lazily switching between watching them both.
Continue Reading...
Let the points on your bezier curve be A and B. Normalize the Vector AB so it has length 1. Let this be AB_norm. Then use asin(AB_norm.y) or acos(AB_norm.x) to get the angle. An angle of 0 degrees is a horizontal vector to the right, then. C-style pseudocode follows:
get_angle(Point A, Point B) {
AB.x = B.x - A.x;
AB.y = B.y - A.y;
length = sqrt(AB.x * AB.x + AB.y * AB.y);
AB_norm.y /= AB.y / length;
angle = asin(AB_norm.y);
// or
// AB_norm.x /= AB.x / length;
// angle = acos(AB_norm.x);
}
angle = get_angle(A, B);
glRotatef(angle, 0.0f, 0.0f, 1.0f);
// Draw the chain link here
You need some math here. You can find tangent, normal, and binormal vectors, and then you can find the angle. If you are still interested let me know, I have some details on this topic.