Ooops
April 02, 2020, 09:35:24 AM

### Author Topic: 3d vectors math help  (Read 921 times)

#### RemiD

• Hero Member
• Posts: 1039
##### 3d vectors math help
« on: October 11, 2019, 05:52:23 PM »
hi,

let me explain briefly the situation, and what i want to achieve :
i have a surface made of vertices and triangles (let say a cube of 1w1h1d...)
each triangle has a 3d normal (=3d vector with a size of 1.0 divided in the 3 axises, example +0.2x,-0.5y,+0.3z)
i have uvmapped this surface, so that the texel size is 0.1u (for a lightmap texture)
so for the 1w1h1d cube, i have a 10x10texels area on each "face" (square made of 2 triangles pointing in a direction)
i have coded a procedure to calculate/store for each texel :
its 2d texture position : TX,TY
its 3d world position : WX,WY,WZ
its 3d normal vector (direction) : NX,NY,NZ

until now i planned to position each shape without rotating it, so that i don't have to recalculate each texel 3d position and 3d normal.

however, as you may know me, i like some challenge

so to calculate a new 3d position (after rotating the mesh), of a 3d point on a mesh (before rotating the mesh), i think that i can use tformpoint() and it should work (not tested yet)

but to calculate a new 3d normal vector (after rotating the mesh), of a 3d normal vector (before rotating the mesh), i have no idea, is there a magic formula to do that with 3d vector maths ?
i know the 3d normal vector before rotating
i know the mesh orientation before rotating (that i can probably translate to a 3d vector using tformvector())
i know the mesh orientation after rotating (that i can probably translate to a 3d vector using tformvector())

any idea ? thanks
DualCore AMD E-450, 1646 MHz - 6 Go DDR3 1333 SDRAM - AMD Radeon HD 6320 Graphics (384 Mo) - Windows 7 Home Premium - DirectX 11.0

#### Matty

• Hero Member
• Posts: 923
##### Re: 3d vectors math help
« Reply #1 on: October 11, 2019, 06:27:14 PM »
Hi RemiD.  You want to use the tformnormal command which is very similar tformpoint.

Plug in the original normals of a non rotated entity and then convert between its local space and the world space after you have performed the rotation.

Does that make sense to you?

#### RemiD

• Hero Member
• Posts: 1039
##### Re: 3d vectors math help
« Reply #2 on: October 11, 2019, 07:03:25 PM »
@Matty>>mmm, let me rephrase it and you tell me if it is what you mean :

the mesh is not rotated (0pitch, 0yaw, 0roll)
i know the texel normal (same as the triangle normal it is in)
i rotate the mesh
i use tformnormal(texelnx,texelny,texelnz,mesh,0)
newnx = tformedx() : newny = tformedy() : newnz = tformedz()
and this should convert the not rotated texel normal to the rotated texel normal ?

if yes, this was easy, i will try asap. thanks
DualCore AMD E-450, 1646 MHz - 6 Go DDR3 1333 SDRAM - AMD Radeon HD 6320 Graphics (384 Mo) - Windows 7 Home Premium - DirectX 11.0

#### GaborD

• Sr. Member
• Posts: 329
##### Re: 3d vectors math help
« Reply #3 on: October 11, 2019, 07:16:10 PM »
In case the other thing doesn't work out, a generic rotation matrix for 3D is something like this:

mat3 rot(vec3 ang) {
mat3 x = mat3(1.0 ,0.0, 0.0, 0.0, cos(ang.x), -sin(ang.x), 0.0, sin(ang.x), cos(ang.x));
mat3 y = mat3(cos(ang.y), 0.0, sin(ang.y), 0.0, 1.0, 0.0, -sin(ang.y), 0.0, cos(ang.y));
mat3 z = mat3(cos(ang.z), -sin(ang.z), 0.0, sin(ang.z), cos(ang.z), 0.0, 0.0, 0.0, 1.0);
return x*y*z;
}

Obviously, that's the GLSL version, but it should be easy to convert.

#### RemiD

• Hero Member
• Posts: 1039
##### Re: 3d vectors math help
« Reply #4 on: October 11, 2019, 09:04:54 PM »
@Gabor>>
DualCore AMD E-450, 1646 MHz - 6 Go DDR3 1333 SDRAM - AMD Radeon HD 6320 Graphics (384 Mo) - Windows 7 Home Premium - DirectX 11.0

#### Steve Elliott

• Hero Member
• Posts: 2300
• elgol developer
##### Re: 3d vectors math help
« Reply #5 on: October 11, 2019, 09:28:49 PM »
lol far too much maths for a Friday.
Windows 10, 64-bit, 16Gb RAM, CPU Intel i5, 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb).
MacOS Catalina, 64-bit, 8Gb RAM, CPU Intel i5, 2.3 Ghz, Intel Iris Plus Graphics 640 1536 MB.
Linux Mint 19.3, 64-bit, 16Gb RAM, CPU Intel i5, 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb).
C64, Raspberry pi 3 and 4.

#### Matty

• Hero Member
• Posts: 923
##### Re: 3d vectors math help
« Reply #6 on: October 11, 2019, 10:10:10 PM »
Yes RemiD. Precisely.

#### RemiD

• Hero Member
• Posts: 1039
##### Re: 3d vectors math help
« Reply #7 on: October 12, 2019, 10:58:44 AM »
@Matty>>ok thanks !
DualCore AMD E-450, 1646 MHz - 6 Go DDR3 1333 SDRAM - AMD Radeon HD 6320 Graphics (384 Mo) - Windows 7 Home Premium - DirectX 11.0

#### RemiD

• Hero Member
• Posts: 1039
##### Re: 3d vectors math help
« Reply #8 on: October 12, 2019, 02:03:18 PM »
ok, i have tested to use tformpoint() to convert the texel world 3d position of a not rotated mesh (0pitch,0yaw,0roll) to the texel world 3d position of a rotated mesh

and to use tformnormal() to convert the texel 3d normal of a not rotated mesh (0pitch,0yaw,0roll) to the texel world 3d normal of a rotated mesh

but it does not seem to work...

Code: [Select]
`;copy the shapeOShapeLSR = CopyMesh(ShapeLSR);copy the ls textureOShapeLST = CopyTexture(ShapeLST) : TextureCoords(OShapeLST,0) : EntityTexture(OShapeLSR,OShapeLST,0,0);change its orientationRotateEntity(OShapeLSR,Rand(-180,+180),Rand(-180,+180),Rand(-180,+180),True);change its positionPositionEntity(OShapeLSR,1.5,1.5,1.5,True);calculate for each texel of the ls texture For I% = 1 To TexelsCount Step 1 OTexelsCount = OTexelsCount + 1 : OI% = OTexelsCount ;same texture 2d position OTexelTX(OI) = TexelTX(I) : OTexelTY(OI) = TexelTY(I) ;new world 3d position wx,wy,wz (using tformpoint()) TFormPoint(TexelWX(I),TexelWY(I),TexelWZ(I),ShapeLSR,OShapeLSR) OTexelWX(OI) = TFormedX() : OTexelWY(OI) = TFormedY() : OTexelWZ(OI) = TFormedZ() ;new world 3d normal nx,ny,nz (using tformnormal()) TFormNormal(TexelNX(I),TexelNY(I),TexelNZ(I),ShapeLSR,OShapeLSR) OTexelNX(OI) = TFormedX() : OTexelNY(OI) = TFormedY() : OTexelNZ(OI) = TFormedZ()Next`i already know that the texels world 3d positions and the texels world 3d normals of the not rotated mesh, are correct...

and i made sure to use rotateentity() and positionentity() so that the rotated mesh has a transform different than 0x,0y,0z 0pitch,0yaw,0roll (with rotatemesh() and positionmesh(), only the vertices are rotated / positionned)

any suggestion ?
thanks
DualCore AMD E-450, 1646 MHz - 6 Go DDR3 1333 SDRAM - AMD Radeon HD 6320 Graphics (384 Mo) - Windows 7 Home Premium - DirectX 11.0

#### STEVIE G

• Sr. Member
• Posts: 377
##### Re: 3d vectors math help
« Reply #9 on: October 12, 2019, 02:49:18 PM »
If the ShapeLSR entity is scaled using scaleentity then the results from tform commands scale by the same.  If you change the ShapeLSR to 0 (for global) when this might work.

S.

#### RemiD

• Hero Member
• Posts: 1039
##### Re: 3d vectors math help
« Reply #10 on: October 12, 2019, 03:32:05 PM »
@StevieG >>the shape is not scaled

i will try to change it to 0 as you say
DualCore AMD E-450, 1646 MHz - 6 Go DDR3 1333 SDRAM - AMD Radeon HD 6320 Graphics (384 Mo) - Windows 7 Home Premium - DirectX 11.0

#### RemiD

• Hero Member
• Posts: 1039
##### Re: 3d vectors math help
« Reply #11 on: October 12, 2019, 03:42:50 PM »
ok it works !

the correct way to write it is :
Code: [Select]
`;new world 3d position wx,wy,wz (using tformpoint()) TFormPoint(TexelWX(I),TexelWY(I),TexelWZ(I),OShapeLSR,0) OTexelWX(OI) = TFormedX() : OTexelWY(OI) = TFormedY() : OTexelWZ(OI) = TFormedZ() ;new world 3d normal nx,ny,nz (using tformnormal()) TFormNormal(TexelNX(I),TexelNY(I),TexelNZ(I),OShapeLSR,0) OTexelNX(OI) = TFormedX() : OTexelNY(OI) = TFormedY() : OTexelNZ(OI) = TFormedZ()`from positionned, rotated, transform of OShape (the new shape), it "projects" the point and the normal which were not positionned (0x0y0z), not rotated (0pitch,0yaw,0roll)

thanks guys !
DualCore AMD E-450, 1646 MHz - 6 Go DDR3 1333 SDRAM - AMD Radeon HD 6320 Graphics (384 Mo) - Windows 7 Home Premium - DirectX 11.0

#### GaborD

• Sr. Member
• Posts: 329
##### Re: 3d vectors math help
« Reply #12 on: October 12, 2019, 03:49:31 PM »
Very cool, great that you got it to work. Very useful commands, make it so much easier than having to do it manually.

#### Kryzon

• Full Member
• Posts: 193
##### Re: 3d vectors math help
« Reply #13 on: October 13, 2019, 06:02:53 AM »
@GaborD in that GLSL rot() function you posted, why is the input angle a vec3 value? Shouldn't it be theta (a scalar), so that all cos/sin use the same angle?

#### GaborD

• Sr. Member
• Posts: 329
##### Re: 3d vectors math help
« Reply #14 on: October 13, 2019, 08:21:06 AM »
It's the three angles for the x, y and z rotations.