DirectX Camera positioning (Third Person View) - c++

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.

Related

Using a MotionController Component in Unreal with C++ instead of Blueprint

After iterating through an array of FMotionControllerSource of an OculusInputDevice IMotionController, I found a connected Oculus Right and Left Touch Controller based on it's ETrackingStatus. With the left and right controllers, I can get the location and rotation using the IMotionController API, which Returns the calibration-space orientation of the requested controller's hand.
Here's a reference to the IMotionController API:
https://docs.unrealengine.com/en-US/API/Runtime/HeadMountedDisplay/IMotionController/index.html
I want to apply the location/rotation to a PosableMesh, so that the mesh is shown where the Oculus controller is in reality. Currently, with the code below the 3D model is displayed down from the camera, so the mapping scale is off. I think WorldToMetersScalemight be off. When I use a small number the controller doesn't move the 3D model much, but this might be messing it up.
FVector position;
FRotator rotation;
int id = tracker.deviceIndex;
FName srcName = tracker.motionControllerSource;
bool success = tracker.motionController->GetControllerOrientationAndPosition(id, srcName, rotation, position, 250.0f);
if (success)
{
poseMesh->SetWorldLocationAndRotation(position, rotation);
}
Adding the camera position to the controller position seemed to fix the issue:
// get camera reference during BeginPlay:
camManager = GetWorld()->GetFirstPlayerController()->PlayerCameraManager;
// TickComponent
poseMesh->SetWorldLocationAndRotation(camManager->GetCameraLocation() + position, rotation);

How do I rotate glm camera

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.

Chipmunk Physics: Rotate body smoothly

I haven't been able to find this after scavenging the forums. I would like to implement something like this ... the main character always moves in the direction it's facing. When the player touches the screen, the character will turn to face that touch location, which should cause the body to move in a different direction.
I can get the character to face a touch location as follows:
CGPoint diff = ccpSub(location, self.position);
CGFloat targetAngle = atan2f(diff.y, diff.x);
self.body->a = targetAngle;
I want something along these lines. Get the current angle the character is facing. Turn that angle into a unit vector. Multiply that unit vector by a max_velocity, and apply it to the character. This should should (theoretically) move the character in the direction it is facing at a constant velocity?
This seems to give me what I want:
cpVect rotatedVel = cpvmult(ccpForAngle(self.body->a), MAX_VELOCITY);
self.body->v = cpvlerpconst(self.body->v, rotatedVel, ACCELERATION * dt);
Now all I need is a way to rotate the character's direction slowly over time. How might I do that?
Sounds like you want to do something like this from Chipmunk's Tank demo:
// turn the control body based on the angle relative to the actual body
cpVect mouseDelta = cpvsub(ChipmunkDemoMouse, cpBodyGetPos(tankBody));
cpFloat turn = cpvtoangle(cpvunrotate(cpBodyGetRot(tankBody), mouseDelta));
cpBodySetAngle(tankControlBody, cpBodyGetAngle(tankBody) - turn);
'turn' is calculated relative to the body's current rotation by transforming the direction vector relative to the body's current rotation. The demo smooths out the rotation using constraints (which you might want to consider here too), but you could also just get away with using cpflerpconst() on 'turn' to get a maximum angular velocity too.
What about using the cpBodySetTorque to set object torque to make it spin/rotate?

C++ Projectile Trajectory

I am using OpenGL to create the 3D space.
I have a spaceship which can fire lasers.
Up until now I have had it so that the lasers will simply to deeper into the Z-axis once fired.
But I am attempting to make a proper aiming system with crosshairs so that you can aim and shoot in any direction, but I have not been successfull in trying to update the laser's path.
I have a directional vector based off the lasers end tip and start tip, which is gotten from the aiming.
How should I update the laser's X,Y,Z values (or vectors) properly so that it looks natural?
I think I see.
Let's say you start with the aiming direction as a 3D vector, call it "aimDir". Then in your update loop add all 3 (x, y and z) to the projectile "position". (OK, at the speed of light you wouldn't actually see any movement, but I think I see what you're going for here).
void OnUpdate( float deltaT )
{
// "move" the laser in the aiming direction, scaled by the amount of time elapsed
// since our last update (you probably want another scale factor here to control
// how "fast" the laser appears to move)
Vector3 deltaLaser = deltaT * aimDir; // calc 3d offset for this frame
laserEndpoint += deltaLaser; // add it to the end of the laser
}
then in the render routine draw the laser from the firing point to the new endpoint:
void OnRender()
{
glBegin(GL_LINES);
glVertex3f( gunPos.x, gunPos.Y, gunPos.z );
glVertex3f( laserEndPoint.x, laserEndPoint.y, laserEndPoint.z );
glEnd();
}
I'm taking some liberties because I don't know if you're using glut, sdl or what. But I'm sure you have at least an update function and a render function.
Warning, just drawing a line from the gun to the end of the laser might be disappointing visually, but it will be a critical reference for adding better effects (particle systems, bloom filter, etc.). A quick improvement might be to make the front of the laser (line) a bright color and the back black. And/or make multiple lines like a machine gun. Feel free to experiment ;-)
Also, if the source of the laser is directly in front of the viewer you will just see a dot! So you may want to cheat a bit and fire from just below or to the right of the viewer and then have in fire slightly up or in. Especially if you have one one each side (wing?) that appear to converge as in conventional machine guns.
Hope that's helpful.

How to make a sprite rotate to a touch Cocos2d

I have an arrow sprite, and it is for aiming purposes in my Cocos2d game. Therefore, I want it to point to where the user touches the screen. How do I program the rotation of the sprite so it will rotate to the user's touch location? Thanks!
These tutorials may be helpful:
http://www.learn-cocos2d.com/knowledge-base/cocos2d-iphone-faq/learn-cocos2d-public-content/manual/cocos2d-general/14826-how-to-rotate-a-sprite-in-a-circular-motion/
http://www.raywenderlich.com/2343/how-to-drag-and-drop-sprites-with-cocos2d
Also, this question is asked (with code) and answered (with more code) here: Rotating Sprite with Touch - Cocos2d
I haven't actually done this before, but I have adapted some of my code (that makes a enemy ship face the player ship) to what you need. Hopefully this is correct.
//rotate to face the touch
CGPoint diff = ccpSub(sprite.position, touch.position);
float angleRadians = atanf((float)diff.y / (float)diff.x);
float angleOffset = CC_DEGREES_TO_RADIANS(90);
if(diff.x < 0)
{
angleRadians += angleOffset;
}
else
{
angleRadians -= angleOffset;
}
PengOne's answer (cool name BTW) was great though and I am voting it up because you should make use of it.