Calculating triangle directions

Blitz3D Forums/Blitz3D Programming/Calculating triangle directions

Neo Genesis10(Posted 2003) [#1]
I have code which explodes a mesh into individual tris and then creates individual particles which replace the original mesh. The trouble is I want the pieces to fall away from the center of the original mesh and I am having troubles calculating which direction they should be travelling in.

Any takers?


Rottbott(Posted 2003) [#2]
Just give them random velocities in the X/Z directions betweeen, say, -10.0 and 10.0. Same in Y except only velocities above 0.0.

Then every frame move the particles by their velocities:

MoveEntity ThisParticle, ThisParticleXV#, ThisParticleYV#, ThisParticleZV#

Turn them so they spin a bit:

TurnEntity ThisParticle, 4, 8, 2

Decrease the Y velocity for gravity so they fall back down:

ThisParticleYV# = ThisParticleYV# - 0.5

And delete them when they get below ground level.

You don't need to mess around calculating angles or anything.


Neo Genesis10(Posted 2003) [#3]
Nice idea, but it doesnt work. Your method would have every triangle moving in the SAME direction (already tested this method). Thus it looks like the object is simply moving.

I need to get the actual DIRECTION of the triangle face in order to accurately move them. I dont know how to calculate this using the vertex normals (yet!). If needed I'll replace MoveEntity with TranslateEntity to move them more accurately.


Shambler(Posted 2003) [#4]
Rottbott's method should work fine.

How are you generating the random vectors?

My guess is that you are using Rnd(1.0) whereas to get the tri's to move in any direction you should use Rnd(-1.0,1.0)

For a more perfect? explosion just calculate each triangles normal from its 3 verts to get the direction vector you need the tri to move.


Neo Genesis10(Posted 2003) [#5]
I am cycling through each individual triangle and creating a new mesh with the vertices in the same place (yes I know there's a lot of surfaces, but its just a tech demo).

I orignally tried Rottbotts method and it had all tris move in the same direction - like I was simply moving the original model. The reason for this is simple - using CreateMesh gives it a default direction of North. Doesnt matter where you add polys, the direction remains constant. Now either I need to position the tris move selectively (and then work out direction) or I just use TranslateEntity and follow the face normals.

This has me baffled currently - there has to be a way to figure a solution. And as mentioned above, Shambler, I dont know how to calculate directions from vertex normals.


Shambler(Posted 2003) [#6]
Ok, then here is a small program to show how to calculate a triangles normal from its vertices...


Graphics 800,600,32


Type tri
Field vx#[3],vy#[3],vz#[3]
Field tnx#,tny#,tnz#
End Type

Global t.tri

createtri(0,0,0 ,0,0,1 ,1,0,1)
createtri(0,0,0 ,1,0,1 ,0,0,1)

While Not KeyHit(1)

Cls

tm=1
For t=Each tri
   For v=1 To 3
      Text 0,tm*50+v*10,"Triangle "+t\vx[v]+" "+t\vy[v]+" "+t\vz[v]
      Text 0,tm*50,"Normal   "+t\tnx+" "+t\tny+" "+t\tnz
   Next
tm=tm+1
Next

Flip

Wend



Function createtri(vx1,vy1,vz1,vx2,vy2,vz2,vx3,vy3,vz3)

;make our triangle
t.tri=New tri
t\vx[1]=vx1
t\vy[1]=vy1
t\vz[1]=vz1
t\vx[2]=vx2
t\vy[2]=vy2
t\vz[2]=vz2
t\vx[3]=vx3
t\vy[3]=vy3
t\vz[3]=vz3

;subtract vectors
ax=t\vx[2]-t\vx[1]
ay=t\vy[2]-t\vy[1]
az=t\vz[2]-t\vz[1]
bx=t\vx[3]-t\vx[2]
by=t\vy[3]-t\vy[2]
bz=t\vz[3]-t\vz[2]

;calculate face normal using cross product
t\tnx=ay*bz-az*by
t\tny=az*bx-ax*bz
t\tnz=ax*by-ay*bx

;normalize it 
;if you are going to use these values for the triangles
;velocities we don't want larger triangles to mean
;faster velocities

l=Sqr(t\tnx*t\tnx+t\tny*t\tny+t\tnz*t\tnz)
t\tnx=t\tnx/l
t\tny=t\tny/l
t\tnz=t\tnz/l

End Function 





Rottbott(Posted 2003) [#7]
My way does work. It moves them in different directions.

Are you actually generating different random velocities for each triangle? That's what you have to do, not one set for the whole thing.

I assure you, it does work. I've used it myself for this exact thing before.


Neo Genesis10(Posted 2003) [#8]
Are you actually generating different random velocities for each triangle? That's what you have to do, not one set for the whole thing.
There are many different meshes, each with 1 triangle. The code breaks down 1 mesh into multiple meshes (dont even get me started on single mesh explosions! lol). Besides, giving them random velocities would only make them move in odd directions rather than outwards.

Since every mesh created using CreateMesh has a default position of 0, 0, 0 and the same as orientation, I need to calculate actual movements. I'll post some code to show you guys later (havent got the disk on me atm!). I did actually do things the same way you apparently have, but they all move the same direction. I want each fragment to move away from the center.

Shamber: Thanks for the code, but I really need to know how to get a direction from that. I have been (so far) unable to find a decent tutorial on vectors so Im wearing the 'noob' cap for this one.


Shambler(Posted 2003) [#9]
The normal which the above routine calculates IS the direction that the triangle is facing.

You can think of the normal as a vector which sticks out of the face, perpendicular to it.

Once you have calculated a triangles face normal you can move the triangle by the x,y and z of that vector and it will move in the direction it is facing.

As easy as MoveEntity tri,t\tnx,t\tny,t\tnz.

So, for example, if you had a sphere made of tri's which is centred on 0,0,0 coords.

You calculate each triangles face normal, then move each triangle using its own face normal vector.

Each triangle will then move away from the centre 0,0,0 which will make the sphere look like it is exploding.

I suggest you give it a go, your almost there, there's nothing more you need to know to impliment it =)


DH(Posted 2003) [#10]
I want the pieces to fall away from the center of the original mesh and I am having troubles calculating which direction they should be travelling in.



Before reading the rest of the posts, i was about to say "Ohhh, so you want to calculate a direction away from a mesh eh? Sounds a bit like a Normal to me!!! LOL" but then I continued to read....

So, for example, if you had a sphere made of tri's which is centred on 0,0,0 coords.

You calculate each triangles face normal, then move each triangle using its own face normal vector.


I think he wants all the triangles to explode from the center of the MESH rather then the direction of its normal. Consider if you had a polygon facing inwards, twords the model center, if it exploded twords its normal direction, it would go to the center of the mesh and not away from the center as he is talking about.

How about this:

Calculate the center of the mesh (using max and Min values for vertex axis positions), lets put this center at, oooo, say 1,2,3. Now take each poly and replace it with a particle (as you described), calculate the vector between it and the model center (normalize it giving us a value between 0-1 for each axis), then on each change in position simply multiply the vector by the needed change, and there you have the desired travel, add that to the current position and whammo...

Orr am I way off base here?


Rottbott(Posted 2003) [#11]
Does it matter for an explosion? It'd be so fast you won't really notice.

Anyway I think it should work if instead of giving each triangle a random velocity, just give it a velocity equal to it's offset from the centre of the mesh.

That means that triangles further from the centre will fly out faster than those in the middle though, so you'll have to divide the resulting velocities by whichever of them is the largest (i.e. a triangle at offset 10.0, 12.0, 4.0 will get velocities of 10.0/12.0, 12.0/12.0 and 4.0/12.0, which works out to 5/6, 1 and 1/3).

If you want you can then multiply all the velocities by some constant scale factor to make them fly away faster or slower.

Anyway, no maths is involved except some simple divisions :-)

EDIT: This is in fact exactly the method Dark Half suggested, and no, you aren't way off base.


Neochrome(Posted 2003) [#12]
theres an example of this exploding sphere in the Blitz examples im sure


Richard Betson(Posted 2003) [#13]
I think the example (on Blitz3D CD) is in Birdie's folder in the 3DSamples as explodemesh.


big10p(Posted 2003) [#14]
Um, I did an exploding mesh demo. It's in the 3D graphics archive on this site - might me of some use to you. Just look for the demo by big10p - it's the only demo I've done! :p