# Multiply Vectors.

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

## Recommended Posts

Hi, I have an question about vectors. In this case I have 2 vectors: Vector3 mRotate; Vector3 mPos; if I have mPos and whant to add mRotate to it how can I do this? I have try to do this. Vector3 result(0,0,0); result.x = (mPos.x * mRotate.x) + (mPos.y * mRotate.y) + (mPos.z * mRotate.z); result.y = (mPos.x * mRotate.x) + (mPos.y * mRotate.y) + (mPos.z * mRotate.z); result.z = (mPos.x * mRotate.x) + (mPos.y * mRotate.y) + (mPos.z * mRotate.z); MoveCamera(result.x, result.z); but It does not working, I do something wrong when I add the vectors. can anyone point me somewere plz.

##### Share on other sites
Well, take a closer look at your code... You're actually giving each element of your result vector the exact same value. [wink]

Multiplying these two vectors would work like this:

Vector3 result(0,0,0);result.x = mPos.x * mRotate.x;result.y = mPos.y * mRotate.y;result.z = mPos.z * mRotate.z;

But now the hidden problem: this is not the way to rotate your camera... Your need to multiply with a matrix to achieve that. I suggest checking out the article section here for more info. It's all there.

hth
CipherCraft

PS This looks like a good start.

[Edited by - CipherCraft on November 30, 2004 9:19:13 AM]

##### Share on other sites
I'm a bit confused as to what mRotate actually is, and what you're actually trying to achieve with this calculation. It looks like you're getting a bit muddled between vectors and matrices, adding and multiplication.

##### Share on other sites
lol ;) I saw that I giving each element of your result vector the exact same value ^^.

but you say I need to use a matrix to get the rotation, well I have looking around but I cant get it to work right.

but If you have time and whant to explain for me It will be great. The problem I have is my mPos(camera pos) is set to the character pos, and when the character moves forward I want to add the RotateAroundPoint vector to my mPos so I can rotate round him when he moves if you know what I mean.

thx.

##### Share on other sites
Rotate function is like this.

void CCamera::Rotate_Position(float rot, float x, float y, float z){	Vector3 vNewPosition;	Vector3 vPos = mPos - mView;	float cosTheta = (float)cos(rot);	float sinTheta = (float)sin(rot);	vNewPosition.x  = (cosTheta + (1 - cosTheta) * x * x)		* vPos.x;	vNewPosition.x += ((1 - cosTheta) * x * y - z * sinTheta)	* vPos.y;	vNewPosition.x += ((1 - cosTheta) * x * z + y * sinTheta)	* vPos.z;	vNewPosition.y  = ((1 - cosTheta) * x * y + z * sinTheta)	* vPos.x;	vNewPosition.y += (cosTheta + (1 - cosTheta) * y * y)		* vPos.y;	vNewPosition.y += ((1 - cosTheta) * y * z - x * sinTheta)	* vPos.z;	vNewPosition.z  = ((1 - cosTheta) * x * z - y * sinTheta)	* vPos.x;	vNewPosition.z += ((1 - cosTheta) * y * z + x * sinTheta)	* vPos.y;	vNewPosition.z += (cosTheta + (1 - cosTheta) * z * z)		* vPos.z;	mRotate.x = mView.x + vNewPosition.x;	mRotate.z = mView.z + vNewPosition.z;}

##### Share on other sites

I'm guessing that you're using one of the LookAt style functions for your camera, ie you give it a camera position and a point to look at, and it sets it up accordingly.

The point to look at should be the point your character is sitting at, which I guess is mPos. But what is RotateAboutPoint? Is it the position of the camera in worldspace? Or is it the vector from mPos to the camera? Or is it something else entirely?

##### Share on other sites
If you change the mRotate in the RotateAroundPoint to mPos it rotate around an point in worldspace. but I whant it to rotate around the character.

character vector is mPlayer.

and I set the camera like this.

xPos.x = mPlayer.x;
xPos.z = mPlayer.z;

mView.x += xPos-mPos.x + 5000.0f;
mView.z += zPos-mPos.z + 5000.0f;
mPos.x += xPos-mPos.x;
mPos.z += zPos-mPos.z;

then I whant to add the rotate vector on top of this to get some sort of rotation around the char.

Im not good at this so if you whant to help me out I will be glad.

##### Share on other sites
Sorry, but all the xPos and yPos and vPos and mRotate and RotateAboutPoint stuff is just confusing the hell out of me, I'm not surprised you're having trouble too.

I think the best thing to do would be to go back to basic principles, since it sounds like you might have overcomplicated things.

Lets define some variables:

vector3 lookAt: // the point you're looking at, i.e the player's coordinates in worldspace.vector3 camera: // the location of the camera in worldspace. This is what you want to know given vLookAt.float   rotateAroundY; // the rotation of the camera about the y axis. for the time being, no up and down rotation, lets keep things simple to begin with. We'll assume this value is already in radians, not degrees.vector3 cameraDirection; // *normalized* direction vector describing the direction the camera is currently pointing.float   zoom; // how close you are to the player. Increase this to make the camera orbit at a higher distance.

Now, if we know lookAt, cameraDirection and zoom, we can work out camera very easily:

// camera = lookAt + (cameraDirection * zoom)// Remember: Camera Direction MUST be normalizedcamera.x = lookAt.x + (cameraDirection.x * zoom); camera.y = lookAt.y + (cameraDirection.y * zoom);camera.z = lookAt.z + (cameraDirection.z * zoom);

So the only tricky bit is working out the cameraDirection.
Something like this should work:

float cosTheta = cosf(rotateAroundY);float sinTheta = sinf(rotateAroundY);// hypoteneuse is always 1 because we're using a normal vector.cameraDirection.x = sinTheta;cameraDirection.y = 0cameraDirection.z = cosTheta;

##### Share on other sites
Allrigt. This is how I did.

zoom = 1.0f;
rotateAroundY += 0.05f * m_timer.elapsed;

lookAt.x = mPlayer.x;
lookAt.y = mPlayer.y;
lookAt.z = mPlayer.z;

camera.x = lookAt.x + (cameraDirection.x * zoom);
camera.y = lookAt.y + (cameraDirection.y * zoom);
camera.z = lookAt.z + (cameraDirection.z * zoom);

float cosTheta = cosf(rotateAroundY);
float sinTheta = sinf(rotateAroundY);

// hypoteneuse is always 1 because we're using a normal vector.

/* RENDER */
gluLookAt(camera.x, camera.y, camera.z,
mView.x, mView.y, mView.z,
mUp.x, mUp.y, mUp.z);

render character...

this only follow the character nothing more or did I setup it wrong?

##### Share on other sites
Quote:
 Original post by X5-ProgrammerAllrigt. This is how I did.this only follow the character nothing more or did I setup it wrong?

I think it might be because you're using cameraDirection before you've set it up properly. (my fault for posting it in a silly order I suppose [grin]) Although depending on the scope of your cameraDirection vector this might not be the problem. Also, the rotation you've defined is fairly slow - ~3 degrees per second. I'd recommend making it a bit faster so it's more obvious whether it's working or not.

You're also using mView in your gluLookAt function, and I have no idea what this is set to. Is it the same as the lookAt vector you've defined earlier?

zoom = 1.0f;rotateAroundY += 0.05f * m_timer.elapsed; lookAt.x = mPlayer.x;lookAt.y = mPlayer.y;lookAt.z = mPlayer.z;// work out the camera direction before calculating the camera positionfloat cosTheta = cosf(rotateAroundY);float sinTheta = sinf(rotateAroundY);// hypoteneuse is always 1 because we're using a normal vector.cameraDirection.x = sinTheta;cameraDirection.y = 0cameraDirection.z = cosTheta;// shouldn't actually need to do this - cameraDirection should already be normalized due to the way we set it up. cameraDirection = Normalize(cameraDirection);// now calculate the camera position..camera.x = lookAt.x + (cameraDirection.x * zoom); camera.y = lookAt.y + (cameraDirection.y * zoom);camera.z = lookAt.z + (cameraDirection.z * zoom);/* RENDER */// I'm assuming mView is the same as the lookAt vector above..?    gluLookAt(camera.x,	camera.y, camera.z,                mView.x,  mView.y,  mView.z,                mUp.x,	mUp.y,	  mUp.z); render character...

1. 1
2. 2
3. 3
Rutin
16
4. 4
5. 5
JoeJ
12

• 10
• 9
• 14
• 10
• 25
• ### Forum Statistics

• Total Topics
632646
• Total Posts
3007637
• ### Who's Online (See full list)

There are no registered users currently online

×