Circular movement or something

Blitz3D Forums/Blitz3D Beginners Area/Circular movement or something

Glorified_Monkey(Posted 2004) [#1]
I have been fooling around with some code to make a square appear as if it is rotating. My main problem is that I don't know how to make the box "slow down" when your seeing the flat side and speed up when your looking at the edge.


Graphics 640,480,16,2
SetBuffer BackBuffer()

Type vertex
     Field x,y,rsp
End Type


Global ver1.vertex=New vertex,ver2.vertex=New vertex,ver3.vertex=New vertex,ver4.vertex=New vertex

While Not KeyHit(1)
Cls

DrawSquare()

Flip

Wend

End

Function DrawSquare(size=100)

If ver1\x=0 Then ver1\x=320-size/2:ver1\y=240-size/2
If ver2\x=0 Then ver2\y=240-size/2+size:ver2\x=320-size/2
If ver3\x=0 Then ver3\x=320-size/2+size:ver3\y=240-size/2
If ver4\x=0 Then ver4\x=320-size/2+size:ver4\y=240-size/2+size

If ver1\x<=320-size/2 Then
     ver1\rsp=1
ElseIf ver1\x>=320+size/2
     ver1\rsp=-1
EndIf
If ver2\x<=320-size/2 Then
     ver2\rsp=1
ElseIf ver2\x>=320+size/2
     ver2\rsp=-1
EndIf
If ver3\x>=320+size/2 Then
     ver3\rsp=-1
ElseIf ver3\x<=320-size/2
     ver3\rsp=1
EndIf
If ver4\x>=320+size/2 Then
     ver4\rsp=-1
ElseIf ver4\x<=320-size/2
     ver4\rsp=1
EndIf


ver1\x=ver1\x+ver1\rsp
ver2\x=ver2\x+ver2\rsp
ver3\x=ver3\x+ver3\rsp
ver4\x=ver4\x+ver4\rsp

Line ver1\x,ver1\y,ver2\x,ver2\y
Line ver2\x,ver2\y,ver4\x,ver4\y
Line ver4\x,ver4\y,ver3\x,ver3\y
Line ver3\x,ver3\y,ver1\x,ver1\y

If ver1\x<320 Then Rect ver1\x,ver1\y,ver3\x-ver1\x,100,1 

End Function

This code is clearly NOT optimizied.

the sides of the square appear to 'bounce' when they reach their limits. Can someone give me formula taht will help this or must I write one?


Stevie G(Posted 2004) [#2]
In work so haven't run your code but it seems like a strange way to simulate a rotating square!? You've obviously got your reasons :) - maybe if you try using floats for x,y,res in your vertex type this will be smoother.

If you want to use the correct rotation formulas let me know.

Stevie


Rob Farley(Posted 2004) [#3]
Graphics 640,480
SetBuffer BackBuffer()

r=0
size=50

Repeat
Cls

sc#=Sin(r)*(size/2)

Rect 320-sc,240-(size/2),sc*2,size
Rect 320+sc,240-(size/2),-sc*2,size,False

r=r+1

Flip
Until KeyHit(1)
To spin it faster make r=r+more or r=r+less to make it slower, you'll need to make r a float if you're moving slower. Or just set r to an abosolute value for any particular angle.


Shambler(Posted 2004) [#4]

Graphics3D 800,600

camera=CreateCamera()
PositionEntity camera,0,0,-2

square=CreateMesh()
squaresurf=CreateSurface(square)

AddVertex squaresurf,-0.5,-0.5,0
AddVertex squaresurf,-0.5,0.5,0
AddVertex squaresurf,0.5,0.5,0
AddVertex squaresurf,0.5,-0.5,0

AddTriangle squaresurf,0,1,2
AddTriangle squaresurf,2,3,0


EntityFX square,16
WireFrame True


While Not KeyHit (1)

TurnEntity square,0,2,0

RenderWorld()
Flip

Wend



If you don't want the diagonal line then texture the quad with a square texture or take out the wireframe true.


Rob Farley(Posted 2004) [#5]
Shambler... I think we're in 2D not 3D.


Shambler(Posted 2004) [#6]
What!? 2D!? what the hell is that? ;)

Just saw it after I posted -.-


Rob Farley(Posted 2004) [#7]
Or... if you want to be really posh...

Graphics 640,480

SetBuffer BackBuffer()

r#=0
size=100

Repeat
Cls
Line 320-(Sin(r)*(size/2)),240-(size/2)-(Cos(r)*(size/8)),320-(Sin(r)*(size/2)),240+(size/2)+(Cos(r)*(size/8))
Line 320-(Sin(r)*(size/2)),240+(size/2)+(Cos(r)*(size/8)),320-(Sin(r+180)*(size/2)),240+(size/2)+(Cos(r+180)*(size/8))
Line 320-(Sin(r+180)*(size/2)),240+(size/2)+(Cos(r+180)*(size/8)),320-(Sin(r+180)*(size/2)),240-(size/2)-(Cos(r+180)*(size/8))
Line 320-(Sin(r+180)*(size/2)),240-(size/2)-(Cos(r+180)*(size/8)),320-(Sin(r)*(size/2)),240-(size/2)-(Cos(r)*(size/8))

r=r+1

Flip

Until KeyHit(1)



Rob Farley(Posted 2004) [#8]
And if you want to be really really flash...

Graphics 640,480

SetBuffer BackBuffer()

r#=0
size=200

Repeat
Cls

drawbox(320,240,100,r)
drawbox(320,240,100,r+90)
drawbox(320,240,200,r+45)
drawbox(120,240,200,-(r/2))
drawbox(520,240,200,-(r/2))

r=r+1

Flip

Until KeyHit(1)



Function drawbox(x,y,size,angle#)
Line x-(Sin(angle)*(size/2)),y-(size/2)-(Cos(angle)*(size/8)),x-(Sin(angle)*(size/2)),y+(size/2)+(Cos(angle)*(size/8))
Line x-(Sin(angle)*(size/2)),y+(size/2)+(Cos(angle)*(size/8)),x-(Sin(angle+180)*(size/2)),y+(size/2)+(Cos(angle+180)*(size/8))
Line x-(Sin(angle+180)*(size/2)),y+(size/2)+(Cos(angle+180)*(size/8)),x-(Sin(angle+180)*(size/2)),y-(size/2)-(Cos(angle+180)*(size/8))
Line x-(Sin(angle+180)*(size/2)),y-(size/2)-(Cos(angle+180)*(size/8)),x-(Sin(angle)*(size/2)),y-(size/2)-(Cos(angle)*(size/8))
End Function



big10p(Posted 2004) [#9]
Hi

There might be something in this that you can use:

	Graphics3D 800,600,16

	SetBuffer BackBuffer()

	SeedRnd MilliSecs()
	
	Type grid_pix
		Field ent
		Field x#,y#,z#
		Field rot#
	End Type
			
	Global frame_count%
	Global fps%
	Global fps_timeout%
	Global frame_time%
	Global slowest_frame%
	Global frame_start%

	Global title_state% = 3
		
	Global cam = CreateCamera()
	PositionEntity cam,0,0,-14
	;TurnEntity cam,10,0,0
	
	Global light = CreateLight()

	Global spiv = CreatePivot()
	Global piv = CreatePivot()
	;RotateEntity piv,0,180,0
	;PositionEntity spiv,0,0,-100
	;EntityParent piv,spiv
	;PositionEntity spiv,0,100,0

	make_titles()	
	
	;RotateEntity piv,-20,0,0

If 1
	sprite = CreateSprite(cam)
	PositionEntity sprite,0,0,1.1
	ScaleSprite sprite,1.5,1
	SpriteViewMode sprite,1
	EntityAlpha sprite,.124
	EntityColor sprite,0,0,0
	EntityBlend sprite,1
	EntityFX sprite,1
	EntityOrder sprite,1
	Dither False
	CameraClsMode cam,0,1
EndIf


	Type part
		Field ent
		Field x#,y#,z#
	End Type

	Const TOTAL_STARS = 400	
	
	make_stars()


	WireFrame 0
	While Not KeyHit(1)
		frame_start = MilliSecs()

		If KeyHit(208) Then title_state = 0
		update_titles()
		update_stars()
		
		UpdateWorld
		If Not KeyDown(14) CopyRect 0,0,800,600,0,0,FrontBuffer(),BackBuffer()
		RenderWorld

		frame_time = MilliSecs() - frame_start	
		show_info()		

		Flip(1)
		
		;Delay 50
		;WaitKey()
	Wend
	
	kill_titles()
	
	End


Global explode_count
Const EXPLODE% = 400
;
; Update title animation
;
Function update_titles()

	Local y#
	
	Select title_state
		Case 0
			title_state = 1
			explode_count = EXPLODE
			
			For this.grid_pix = Each grid_pix
				this\x = Rnd(-1,1)
				this\y = Rnd(-1,1)
				this\z = Rnd(-1,1)
				this\rot = Rnd(-1,1)
			Next		
		Case 1
			For this.grid_pix = Each grid_pix
				TranslateEntity this\ent,this\x,this\y,this\z
				TurnEntity this\ent,this\rot,0,0
				TurnEntity this\ent,0,this\rot,0
				TurnEntity this\ent,0,0,this\rot
			Next
			
			explode_count = explode_count - 1

			If explode_count = 0
				title_state = 2
				explode_count = EXPLODE
			EndIf		
		Case 2
			For this.grid_pix = Each grid_pix
				TranslateEntity this\ent,-this\x,-this\y,-this\z
				TurnEntity this\ent,0,0,-this\rot
				TurnEntity this\ent,0,-this\rot,0
				TurnEntity this\ent,-this\rot,0,0
			Next
				TurnEntity piv,0,-1,0
			
			explode_count = explode_count - 1
			If explode_count = 0 Then title_state = 3				
		Case 3
			y = Abs(EntityYaw(piv))/2.0
			
			TurnEntity piv,0,-(.3 + (Sin(y) * (y/20))),0
			
			If Rand(1,400)=100 Then title_state = 0
	End Select
	
End Function


;
; Create the game title grid of cubes
;
Function make_titles()

	Local title_rows% = 0
	Local title_cols% = 0
	Local grid_row$, datum$
	Local pix.grid_pix
	Local grid_step#
	Local tl_x#, tl_y#
	Local title_width# = 18.0
	Local title_height#
	Local title_ent
	Local pix_size#
	
	title_ent = CreateCube()
	EntityShininess title_ent,1
		
	; Find width and height of title grid from DATA
	Restore TITLE_DATA
	Read grid_row$
	title_cols = Len(grid_row$)
	Repeat
		title_rows = title_rows + 1
		Read grid_row$
	Until grid_row$ = "."
	
	title_height = title_rows * (title_width/title_cols)

	grid_step = title_width/(title_cols-1)
	tl_x = -title_width/2.0
	tl_y = title_height/2.0

	pix_size# = grid_step/2.0 - 0.04
	ScaleEntity piv,pix_size,pix_size,pix_size
	
	Restore TITLE_DATA
	 
	For r = 1 To title_rows
		Read grid_row$

		For c = 1 To title_cols
			datum$ = Mid$(grid_row$,c,1)

			If datum$ <> " "
				pix = New grid_pix
				pix\ent = CopyEntity(title_ent,piv)
				
				PositionEntity pix\ent,tl_x + ((c-1)*grid_step),tl_y - ((r-1)*grid_step),0,True

				Select datum$
					;Case " "
					;	EntityColor pix\ent,0,0,0
					Case "1"
						EntityColor pix\ent,150,50,0
						ScaleEntity pix\ent,1,1,4
					Case "2"
						EntityColor pix\ent,0,150,0
						ScaleEntity pix\ent,1,1,6
					Case "3"
						EntityColor pix\ent,150,150,0
						ScaleEntity pix\ent,1,1,2
					Case "4"
						EntityColor pix\ent,150,0,0
						ScaleEntity pix\ent,1,1,4
					Default
						rgb_step# = 255.0/19.0
						val = Asc(datum$) - Asc("a")
						;DebugLog val
						;DebugLog 255-val*rgb_step#
						EntityColor pix\ent,100,50,0;255,0,255 - val*rgb_step#
				End Select
			EndIf
		Next
	Next
	
	FreeEntity title_ent
	
End Function


;
; Free all mem used by title
;
Function kill_titles()

	For this.grid_pix = Each grid_pix
		FreeEntity this\ent
		Delete this
	Next

End Function


;
; Show debug info
;
Function show_info()
	
	frame_count = frame_count + 1

	If MilliSecs() > fps_timeout Then
		fps_timeout = MilliSecs() + 1000 
		fps = frame_count 
		frame_count = 0 
	EndIf 
	
	If frame_time > slowest_frame Then slowest_frame = frame_time
	
	Color 0,0,100
	Rect 0,0,200,80,1
	Color 255,255,255
	
	Text 10,10," Triangles: " + TrisRendered()
	Text 10,25," Millisecs: " + frame_time
	Text 10,40,"   slowest: " + slowest_frame
	Text 10,55,"       FPS: " + fps

End Function


Function make_stars()

	For n = 1 To TOTAL_STARS
		this.part = New part
		
		this\ent = CreateSprite()
		;ScaleSprite this\ent,5,5
		EntityColor this\ent,255,255,255
		EntityAutoFade this\ent,200,500
	
		init_star(this)
		update_stars()
	Next

End Function


Function kill_stars()

	For this.part = Each part
		FreeEntity this\ent
		Delete this
	Next

End Function


Function update_stars()

	For this.part = Each part

		If Not EntityInView(this\ent,cam)
			init_star(this)
		Else
			TranslateEntity this\ent,this\x,this\y,this\z
		EndIf
	Next

End Function


Function init_star(star.part)

	r#=Rnd(0.0,360.0)
	star\x = Cos(r)/2
	star\y = Sin(r)/2
	star\z = -Rnd(.01,.5)

	PositionEntity star\ent,star\x*50,star\y*50,500

End Function


.TITLE_DATA
Data " ttttttttttttttttttttttttttttttttttttttttt "
Data "t                                         t"
Data "t 111 111 111 111 111     111 1 1   1 111 t"
Data "t 1   1 1 1 1 1   1        1  1 11 11 1   t"
Data "t 111 111 111 1   11  111  1  1 1 1 1 11  t"
Data "t   1 1   1 1 1   1        1  1 1   1 1   t"
Data "t 111 1   1 1 111 111      1  1 1   1 111 t"
Data "t                 2222                    t"
Data "t 1 1  1 1   1  22222222  11  111 111 111 t"
Data "t 1 11 1 1   1 2244224422 1 1 1   1 1 1   t"
Data "t 1 1 11  1 1  2222222222 1 1 11  11  111 t"
Data "t 1 1  1  1 1    22  22   1 1 1   1 1   1 t"
Data "t 1 1  1   1    22 22 22  111 111 1 1 111 t"
Data "t              2        2                 t"
Data " ttttttttttttt      3     tttttttttttttttt "
Data "                   3                       "
Data "                    3                      "
Data "                   3                       "
Data "."


...then again, I've been to the pub and am a bit p@~#ed. Something i knocked up aged ago.


Stevie G(Posted 2004) [#10]
@big10p Cool demo mate - you should stick this in the code archives.


BlackJumper(Posted 2004) [#11]
.... and I thought Rob was showing off !

Excellent demo.


big10p(Posted 2004) [#12]
Oh, cheers guys! :)

If you really think it's worth it then I'll try and clean the code up a bit and put it in the archives. Oooh, don't you just hate revisting old code! :/