Rotating a set of 4 verts

Blitz3D Forums/Blitz3D Programming/Rotating a set of 4 verts

EOF(Posted 2003) [#1]
Anybody got a (small) algorithm which can rotate a set of four vertices?
Here is what I wish to do:

The routine only needs to know about the 4 verts. I need to be able to rotate them any angle from 0 to 360 degrees.

In case you are wondering why I don't just use 'RotateEntity' I am looking into creating a single-surface version of SpriteControl so I need to be able to rotate only a set of 4 verts within a mesh.

Here is some code to get you started. It just creates a quad and displays it on screen. I have references to the four verts - vx0/vy0 , vx1/vy1 ...
; Rotate 4 quad verts
; vertices in quad placed like so:

;    0 --------- 1
;     |         |
;     |         |
;     |         |
;     |         |
;    2 --------- 3

; set up 3d display -> gfxmode/camera/sprite pivot
Const gw=640,gh=480

Graphics3D gw,gh
SetBuffer BackBuffer()
spritecamera=CreateCamera()
spritepivot=CreatePivot(spritecamera)
aspect#=Float(gh)/Float(gw) : scale#=2.0/gw
PositionEntity spritepivot,-1,aspect,1.0
ScaleEntity spritepivot,scale,scale,scale
CameraClsColor spritecamera,190,160,20
WireFrame True

quad=CreateQuad(140,136,spritepivot)
DrawQuad quad,gw/3,gh/3

s=GetSurface(quad,1)
vx0#=VertexX(s,0) : vy0#=VertexY(s,0) ; top/left vert
vx1#=VertexX(s,1) : vy1#=VertexY(s,1) ; top/right vert
vx2#=VertexX(s,2) : vy2#=VertexY(s,2) ; bottom/left vert
vx3#=VertexX(s,3) : vy3#=VertexY(s,3) ; bottom right vert

RenderWorld
Flip
WaitKey
End

; create sprite quad
Function CreateQuad(w,h,par)
	; Create quad mesh
	Local sprite=CreateMesh(par)
	Local s=CreateSurface(sprite)
	AddVertex s,0,0,0 ,0,0 : AddVertex s,2,0,0 , 1,0
	AddVertex s,0,-2,0 ,0,1 : AddVertex s,2,-2,0 , 1,1
	AddTriangle s,0,1,2 : AddTriangle s,3,2,1
	EntityFX sprite,1+16 : EntityOrder sprite,-100
	ScaleEntity sprite,Float(w)/2,Float(h)/2,1
	Return sprite
End Function

; Position 3d sprite at 2D screen coordinates
Function DrawQuad(sprite,x,y,z=0)
	PositionEntity sprite,x+0.5,-y+0.5,z
End Function



EOF(Posted 2003) [#2]
I should point out that the axis/handle of the quad should be allowed to be placed anywhere (like the 2D RotateImage command).


Warren(Posted 2003) [#3]
I did something similar to this by creating a pivot (once at program start up), rotating the pivot and then using TFormPoint (or whatever it's called) to transform the vertices from the pivots space into world space. Seems to work pretty good.


EOF(Posted 2003) [#4]
EpicBoy, I think I'm going to kick myself. Reading your post made me think:
I could just duplicate the verts into a separate quad, rotate that, then position the original verts where the rotated quad ones are!