Quad Rotation

Blitz3D Forums/Blitz3D Programming/Quad Rotation

Nexic(Posted 2005) [#1]
I have been using some single surface code I found either in a code archives on posted on these boards some time ago for my latest game. But I now need one of the quads in the system to rotate for a certain effect. Its a bit late in the project to completely change the system so I am planning to modify what is already there to take into account an angle variable at the end of each frame when it faces towards the camera. The angle will be Z (roll) or the angle you usually change with RotateSprite (although I think rotatesprite is iverted Z).

My 3D math skills are pretty much non existent, so I'd like to ask if anyone could either give me some code, or some steps to get me started. Here is what the system does at the end of each frame to make the quad face the camera.

For s.sprite=Each sprite
		If s\life>0 And s\visible>0
			s\life#=s\life#-(1.0*FL\SpeedFactor)			
			s\x=s\x+(s\vx*FL\SpeedFactor)
			s\y=s\y+(s\vy*FL\SpeedFactor)
			s\z=s\z+(s\vz*FL\SpeedFactor)
			s\s=s\s+(s\vs*FL\SpeedFactor)
			s\a=s\a+(s\va*FL\SpeedFactor)
	
			TFormPoint s\x,s\y,s\z,0,camera\model
			If TFormedZ#()>0
				TFormVector -s\s,-s\s,0,camera\model,0 
				VertexCoords s\surf,s\verti[0],s\x+TFormedX#(),s\y+TFormedY#(),s\z+TFormedZ#()
				TFormVector s\s,-s\s,0,camera\model,0 
				VertexCoords s\surf,s\verti[1],s\x+TFormedX#(),s\y+TFormedY#(),s\z+TFormedZ#()
				TFormVector -s\s,s\s,0,camera\model,0 
				VertexCoords s\surf,s\verti[2],s\x+TFormedX#(),s\y+TFormedY#(),s\z+TFormedZ#()
				TFormVector s\s,s\s,0,camera\model,0 
				VertexCoords s\surf,s\verti[3],s\x+TFormedX#(),s\y+TFormedY#(),s\z+TFormedZ#()
				VertexColor s\surf,s\verti[0],s\r,s\g,s\b,s\a
				VertexColor s\surf,s\verti[1],s\r,s\g,s\b,s\a
				VertexColor s\surf,s\verti[2],s\r,s\g,s\b,s\a
				VertexColor s\surf,s\verti[3],s\r,s\g,s\b,s\a
			EndIf
			
			dbug=dbug+1
		EndIf
		If s\life <= 0 
		 HideSprite(Handle(s))
		EndIf
	Next



x = x pos
y = y pos
z = z pos
a = alpha
s = scale
r = red
g = green
b = blue
life = time until death
visible = err visible or not

I'd like to change what is there to take into account a new angle variable within the type.

Thanks in Advance :)


Nexic(Posted 2005) [#2]
I know this is a rather complex problem, but I'm not really asking for a solution, just a link to another helpful topic would be good :)

Pleaaaseeee ^.^


Nexic(Posted 2005) [#3]
Well I've been working on it, using a few examples I found posted in old topics and I've managed to write this, but wierd things keep happening with my sprites. If an angle of zero is used then they work fine, except sometimes they appear in the wrong places. If a value of more than zero is specified for angle then all sorts of random wierdness begins to occur. With the sprites appearing in completely wrong places.

RotateEntity ss_pivot,0,0,s\angle#
	         For i=0 To 3
	          vx# = VertexX(s\surf,s\verti[i])
			  vy# = VertexY(s\surf,s\verti[i])
			  vz# = VertexZ(s\surf,s\verti[i])
			  nx# = VertexNX(s\surf,s\verti[i])
			  ny# = VertexNY(s\surf,s\verti[i])
			  nz# = VertexNZ(s\surf,s\verti[i])

		      TFormPoint vx#,vy#,vz#,ss_pivot,0
		      VertexCoords s\surf,s\verti[i],s\x+TFormedX(),s\y+TFormedY(),s\z+TFormedZ()		
		      TFormNormal nx#,ny#,nz#,ss_pivot,0
		      VertexNormal s\surf,s\verti[i],TFormedX(),TFormedY(),TFormedZ()


Anyone got any ideas at all?


Stevie G(Posted 2005) [#4]
Ok, I think you're on the right lines. Don't have Blitz at work but .... If you parent a pivot to the camera, Rotate the pivot on it's roll axis using the global world ref , rotateenitty Pivot, 0 , 0, angle, true

Then do your transformation commands on this pivot instead of the camera to face the camera .. should work.

Stevie


Nexic(Posted 2005) [#5]
I can see why what you suggested should work, but if I put anything other than zero in the roll for the pivot, the sprites seem to dissappear. If I put values in the pivot's yaw and pitch, I seems as if the sprites changing their own angles in the way you'd expect - its just roll that seems to mess it all up.

Here's what I have now:
 RotateEntity camera\pivot,0,0,s\angle#, True

			TFormPoint s\x,s\y,s\z,0,camera\pivot
			If TFormedZ#()>0
			 TFormVector -s\s,-s\s,0,camera\pivot,0 
			 VertexCoords s\surf,s\verti[0],s\x+TFormedX#(),s\y+TFormedY#(),s\z+TFormedZ#()
			 TFormVector s\s,-s\s,0,camera\pivot,0 
			 VertexCoords s\surf,s\verti[1],s\x+TFormedX#(),s\y+TFormedY#(),s\z+TFormedZ#()
			 TFormVector -s\s,s\s,0,camera\pivot,0 
			 VertexCoords s\surf,s\verti[2],s\x+TFormedX#(),s\y+TFormedY#(),s\z+TFormedZ#()
			 TFormVector s\s,s\s,0,camera\pivot,0 
			 VertexCoords s\surf,s\verti[3],s\x+TFormedX#(),s\y+TFormedY#(),s\z+TFormedZ#()
			 VertexColor s\surf,s\verti[0],s\r,s\g,s\b,s\a
			 VertexColor s\surf,s\verti[1],s\r,s\g,s\b,s\a
			 VertexColor s\surf,s\verti[2],s\r,s\g,s\b,s\a
			 VertexColor s\surf,s\verti[3],s\r,s\g,s\b,s\a
		    EndIf



Is this what you meant me to do? (camera\pivot is parented to the actual camera)


Nexic(Posted 2005) [#6]
Meh, I played around with the rotateentity command, and when I put RotateEntity(camera\pivot, 0, 180, s\angle#) it seemed to work. I guess for some reason the sprites were facing away from pivot instead of towards it with 0 for Y axis.

Thanks for the help steve, you saved my butt again!


Stevie G(Posted 2005) [#7]
In which case ... you're vertex positions or triangle vertex numbers ( when you create the polys for the quad) are the wrong way round ... just change these and you won't have to rotate 180 about the yaw axis.

Happy to help when I can :) You do know that you can get away with only 2 tformvector commands to position the vertex's though .. if you like I'll send you some code later.


Nexic(Posted 2005) [#8]
As I mentioned earlier I didn't actually write the code to point the quad towards the camera, so I'm really even sure how to invert it without messing things up. If you don't mind then I'd certainly like it if you could send my some cleaned up code:

neil at jaggedbladesoft dot com

Thanks :)