Mirroring a vector

Blitz3D Forums/Blitz3D Programming/Mirroring a vector

big10p(Posted 2006) [#1]
Given a vector, anyone know the quickest way to mirror it, using maths?

Let's use 2D (x,y) to make it simpler: say I have a vector x=-1 y=1 and I want to mirror it so it becomes x=1 y=1. In this instance I can just negate the x but I want it to work whatever the vector. e.g. with x=0 y=1, negating the x makes no difference, I want it to become x=0 y=-1.

I know I could use TFormVector on a pivot or something, but I want to find a quicker(?) way using maths.

Cheers.

[edit]Yikes. I guess this isn't going to be a simple as I'd hoped because I need to figure into the equation the axis I want to mirror around. :/


Stevie G(Posted 2006) [#2]
I think you're talking about something bouncing off a wall or suchlike? In which case you can derive the reflection vector by determining the cross product of the motion vector and the collision normal vector.

Example here ..

http://www.blitzbasic.com/codearcs/codearcs.php?code=670

Otherwise, I'm not sure what you mean?

Stevie


BlackJumper(Posted 2006) [#3]
... as per your edit - you can't 'mirror' something without defining the axis or plane that you intend to act as the mirror

have a look at this FAQ...

http://web.archive.org/web/20041011193859/http://www.j3d.org/matrix_faq/vectfaq_latest.html


big10p(Posted 2006) [#4]
Thanks for the links, guys - I'll take a look.

What I need this for is this: I am creating a 'symetrical' mesh on-the-fly. Because it's symetrical, I calculate the normal of a vert on the left side, and want to simply mirror it to apply to the corresponding vert on the right side. I'd hoped doing this would be quicker that calculating each normal from scratch. Does that make sense?

Anyway, it turns out I may just be better off using UpdateNormals and be done with it. Not sure - got a bit more experimenting to do. :)

Thanks again.


Stevie G(Posted 2006) [#5]
Ross C has something that does this in the archives. Quite recent in fact ...

http://www.blitzbasic.com/codearcs/codearcs.php?code=1521


DJWoodgate(Posted 2006) [#6]
Also if you want rotation around am arbitary axis, not necessarily one aligned to xyz, there is an example of that in the matrix section of Marks published 3d c++ geometry code. I have used something similar myself when playing around with rotating objects embedded in meshes. Not sure if it would help in your case or not though.


	Matrix( float angle,const Vector &axis ){
		const Vector &u=axis;
		float c=cosf(angle),s=sinf(angle);
		float x2=axis.x*axis.x,y2=axis.y*axis.y,z2=axis.z*axis.z;
		i=Vector( x2+c*(1-x2),u.x*u.y*(1-c)-u.z*s,u.z*u.x*(1-c)+u.y*s );
		j=Vector( u.x*u.y*(1-c)+u.z*s,y2+c*(1-y2),u.y*u.z*(1-c)-u.x*s );
		k=Vector( u.z*u.x*(1-c)-u.y*s,u.y*u.z*(1-c)+u.x*s,z2+c*(1-z2) );
	}


Ps. do not ask me to explain the maths! I just know it works.


big10p(Posted 2006) [#7]
Stevie: Yeah, I need to mirror on an arbitary axis so Ross' code doesn't help, I'm afraid.

David: Gulp! Thanks for the code. I'll see if I can make use of it but judging by the heavy maths involved, I can't see it being any faster than simply calculating each normal from scratch. I didn't think to look at Mark's geometry code.

Cheers


Stevie G(Posted 2006) [#8]
Big10p .. was it not you who did the meshclip thing?

Surely you could use that reasonably easily? Clip the mesh on an arbitary plane, copy and transform the vertices/tris to the other side of the plane. Then addmesh them together?

Stevie


Floyd(Posted 2006) [#9]
Does the mesh really have to be created with an arbitrary orientation?

What about creating it in the usual way, aligned to the coordinate axes, so reflections are trivial.
Then, after the mesh is complete, use RotateMesh to put it in the desired orientation.


big10p(Posted 2006) [#10]
Sorry, I haven't explained myself very well. The mesh I'm creating, as a whole, isn't symetrical. However, it's made up of symetrical pieces.

I don't have a problem creating the mesh, or calculating the vert normals, it's just that normals on one side of each 'peice' are simply a mirror of those on the other side. I just wondered if there was an easy/fast way of mirroring the normals instead of re-calculating from scratch.

The mesh is (or can) be made up of many 'peices' so rotating each piece to axis-align it, then rotate it back would be too slow, I think.


DJWoodgate(Posted 2006) [#11]
All things considered then, recalculate the normals. :)
Here is a link to some maths on reflection.
http://www.euclideanspace.com/maths/geometry/affine/reflection/

Stevie G's original suggestion about the bouncing code merits further investigation since you have a plane normal and your vertex normal which might somehow be reflected onto the other side of the plane using the inverse of the plane normal? I don't know,I would have to play about with it.

Edit, no I do not think that will work. So to reflect a point or normal you could use a reflection matrix as given in the link above. This may be less work than recalculating the normals for a lot of points to be reflected in the same plane, but I suspect there is not going to be a lot in it and you always loose a little bit of accuracy when doing these transforms.

; given a plane normal
pnx#=-0.5
pny#=0.866
pnz#=0

; construct a reflection matrix
pnx2#=pnx*pnx
pny2#=pny*pny
pnz2#=pnz*pnz

mat1# = -pnx2+pnz2+pny2
mat2# = -2*pnx*pny
mat3# = -2*pnx*pnz
mat4# = -2*pny*pnx
mat5# = -pny2+pnx2+pnz2
mat6# = -2*pny*pnz
mat7# = -2*pnz*pnx
mat8# = -2*pnz*pny
mat9# = -pnz2+pny2+pnx2


; given a point to reflect
px#=1
py#=0
pz#=0

; multiply the point by the reflection matrix
rx# = px*mat1 + py*mat2 + pz*mat3
ry# = px*mat4 + py*mat5 + pz*mat6
rz# = px*mat7 + py*mat8 + pz*mat9




big10p(Posted 2006) [#12]
I was looking at that site you linked to yesterday, David. ;) Didn't notice the page about reflection, though, thanks.

That site seems very good but I get lost pretty quickly when the maths gets too complicated, and I lose sight of the logic of what's happening. Then again, I don't really need to know how things work, just that they do work. :)

Thanks for the code. I suspect it may not be any quicker than recalculating the nromal, though, as I'd need to re-calculate the reflection matrix for each 'peice' of my mesh. Hmm, It maybe useful, though - I'll have to see what I can do. :)