Разработчик
Регистрация: 18.10.2007
Сообщений: 349
Написано 64 полезных сообщений (для 95 пользователей)
|
Функции работы с геометрией для игр
Вот решил вернуться временно к BlitzMax.
Создаю функции работы с геометрией для 2d игр.
Пока применения им нету,
но потом создам более мощные функции,
например для определения столкновений.
Пока только:
1: Определение дистанции
2: Нормирование
3: Проекция
(получение нового вектора,
с помощью проецирования другово вектора на прямую)
Статья: http://noregret.org/tutor/n/collision/
Очень хорошая статья

''''''''''''''''''''''''''''''Проект''''''''''''''''''''''''''''''
SuperStrict
SeedRnd(MilliSecs())
AppTitle$ = "GEOMETRY FUNCTIONS VERSION 1.0"
''''''''''''''''''''''''''''''Типы''''''''''''''''''''''''''''''
Global azgf_function : String[3]
Global azgf_forms : Byte[3]
Global azgf_form : tzgf_form[4]
Type tzgf_form
Field f: Byte
Field x: Short
Field y: Short
Field w: Short
Field h: Short
Field a: Short
Field r: Byte
Field g: Byte
Field b: Byte
Field s: Byte
Method Update( _c: Byte )
If point_d = 1 And MouseX()>x-w And MouseX()<x+w And MouseY()>y-h And MouseY()<y+h
point_n=_c
End If
If point_n=_c And point_d>0
x=MouseX()
If x<(gfx_width-gfx_height) Then x=gfx_width-gfx_height
If x>gfx_width Then x=gfx_width
y=MouseY()
If y<0 Then y=0
If y>gfx_width Then y=gfx_width
End If
If x<0 Then x = 0
If y<0 Then y = 0
If x>gfx_width Then x = gfx_width
If y>gfx_height Then y = gfx_height
End Method
Method Draw( _c: Byte )
SetColor r,g,b
If f=1
DrawRect x-w,y-h,w*2,h*2
DrawText _c,x+w,y+h
End If
End Method
Function Create: tzgf_form( _f: Byte, _x: Short, _y: Short, _w: Short, _h: Short, _a: Short, _r: Byte, _g: Byte, _b: Byte, _s: Byte )
Local _obj: tzgf_form= New tzgf_form
_obj.f= _f
_obj.x= _x
_obj.y= _y
_obj.w= _w
_obj.h= _h
_obj.a= _a
_obj.r= _r
_obj.g= _g
_obj.b= _b
_obj.s= _s
Return _obj: tzgf_form
End Function
End Type
''''''''''''''''''''''''''''''Переменные''''''''''''''''''''''''''''''
Global menu: Byte= False
Global gfx_width: Int= 640
Global gfx_height: Int= 480
Global gfx_widthdiv: Int= gfx_width/2
Global gfx_heightdiv: Int= gfx_height/2
Global gfx_depth: Int= 32
Global gfx_hertz: Int= 75
Global time_old: Long
Global time_new: Short
Global time_next: Long
Global time_nrnd_max: Short
Global time_nrnd_min: Short
Global time_nfps: Short
Global time_ornd_max: Short
Global time_ornd_min: Short
Global time_ofps: Short
Global point_d: Byte= 0
Global point_m: Byte= False
Global point_r: Byte= False
Global point_f: Byte= 0
Global point_n: Byte= 255
Global n: Short= 0
''''''''''''''''''''''''''''''Загрузка''''''''''''''''''''''''''''''
azgf_function[0]="distance"
azgf_function[1]="normalization"
azgf_function[2]="projection"
azgf_forms[0]=2
azgf_forms[1]=3
azgf_forms[2]=4
fzgf_create( )
''''''''''''''''''''''''''''''Установка графики''''''''''''''''''''''''''''''
SetGraphicsDriver GLMax2DDriver()
Graphics gfx_width , gfx_height , 0 , 0
AutoMidHandle(True)
ShowMouse()
SetClsColor 127,127,127
''''''''''''''''''''''''''''''Главный цикл''''''''''''''''''''''''''''''
While Not KeyDown(KEY_ESCAPE)
time_new=(MilliSecs()-time_old)
time_old=MilliSecs()
If time_next < time_old
time_next=time_old/1000*1000+1000
time_ornd_max=time_nrnd_max
time_ornd_min=time_nrnd_min
time_ofps=time_nfps
time_nrnd_max=0
time_nrnd_min=65535
time_nfps=0
End If
time_nfps:+1
If time_nrnd_max < time_new Then time_nrnd_max = time_new
If time_nrnd_min > time_new Then time_nrnd_min = time_new
Cls
If MouseDown(MOUSE_LEFT) And point_d=0 Then point_d=1
If Not MouseDown(MOUSE_LEFT)
point_d=0
point_n=255
End If
For n = 0 Until azgf_forms[point_f]
If azgf_form[n].s= 1 Or azgf_form[n].s= 3 Then azgf_form[n].Update(n)
Next
fzgf_update( )
For n = 0 Until azgf_forms[point_f]
If azgf_form[n].s> 1 Then azgf_form[n].Draw(n+1)
Next
If point_d=1 Then point_d=2
SetColor 255,255,255
DrawLine gfx_width-gfx_height,0,gfx_width-gfx_height,gfx_height
DrawText "GEOMETRY FUNCTIONS" , 0 , 0
DrawText "VERSION 1.0" , 0 , 10
DrawText "AUTHOR: ZHELEZNOV" , 0 , 20
DrawText " ANDREY IVANOVICH" , 0 , 30
DrawText "SET FUNCTION" , 0 , 50
DrawText " WITH KEY TAB" , 0 , 60
DrawText " CURRENT IS "+point_f , 0 , 70
DrawText " NAME IS " , 0 , 80
DrawText " "+azgf_function[point_f] , 0 , 90
If KeyHit(KEY_TAB)
point_f:+1
If point_f>2 Then point_f=0
fzgf_create( )
End If
DrawText "FRAME: " + time_ornd_Min + "ms-" + time_ornd_max + "ms" , 0 , gfx_height-30
DrawText "FPS: " + time_ofps + "frame/s" , 0 , gfx_height-20
Flip
Wend
End
Function fzgf_create( )
Select point_f
Case 0
azgf_form[0]= tzgf_form.Create( 1,300,100,5,5,0,255,0,0,3 )
azgf_form[1]= tzgf_form.Create( 1,400,200,5,5,0,0,0,255,3 )
Case 1
azgf_form[0]= tzgf_form.Create( 1,300,100,5,5,0,255,0,0,3 )
azgf_form[1]= tzgf_form.Create( 1,400,200,5,5,0,0,0,255,3 )
azgf_form[2]= tzgf_form.Create( 1,200,0,5,5,0,0,255,0,2 )
Case 2
azgf_form[0]= tzgf_form.Create( 1,300,200,5,5,0,255,0,0,3 )
azgf_form[1]= tzgf_form.Create( 1,400,200,5,5,0,255,0,0,3 )
azgf_form[2]= tzgf_form.Create( 1,350,100,5,5,0,0,0,255,3 )
azgf_form[3]= tzgf_form.Create( 1,200,0,5,5,0,0,255,0,2 )
End Select
End Function
Function fzgf_update( )
Local _x1:Int, _y1:Int, _x2:Int, _y2:Int, _xxpyy:Float, _distance:Double
SetColor 255,255,255
Select point_f
Case 0
DrawText "x1="+azgf_form[0].x , 0 , 120
DrawText "y1="+azgf_form[0].y , 0 , 130
DrawText "x2="+azgf_form[1].x , 0 , 140
DrawText "y2="+azgf_form[1].y , 0 , 150
_x1=azgf_form[1].x-azgf_form[0].x
DrawText "x=x2-x1="+_x1 , 0 , 160
_y1=azgf_form[1].y-azgf_form[0].y
DrawText "y=y2-y1="+_y1 , 0 , 170
DrawText "DISTATION=" , 0 , 180
DrawText " =sqrt(x*x+y*y)=" , 0 , 190
DrawText " =sqrt("+_x1+"*"+_x1+"+" , 0 , 200
DrawText " +"+_y1+"*"+_y1+")=" , 0 , 210
_xxpyy=_x1*_x1+_y1*_y1
DrawText " =sqrt("+_xxpyy+")=" , 0 , 220
_distance=Sqr(_xxpyy)
DrawText " ="+_distance , 0 , 230
Case 1
DrawText "x1="+azgf_form[0].x , 0 , 120
DrawText "y1="+azgf_form[0].y , 0 , 130
DrawText "x2="+azgf_form[1].x , 0 , 140
DrawText "y2="+azgf_form[1].y , 0 , 150
_x1=azgf_form[1].x-azgf_form[0].x
DrawText "x=x2-x1="+_x1 , 0 , 160
_y1=azgf_form[1].y-azgf_form[0].y
DrawText "y=y2-y1="+_y1 , 0 , 170
DrawText "DISTATION=" , 0 , 180
DrawText " =sqrt(x*x+y*y)=" , 0 , 190
_xxpyy=_x1*_x1+_y1*_y1
_distance=Sqr(_xxpyy)
DrawText " ="+_distance , 0 , 200
If _distance<>0
azgf_form[2].x=azgf_form[0].x+_x1/_distance*100
azgf_form[2].y=azgf_form[0].y+_y1/_distance*100
DrawText "x3=x1+x/DIST*100=" , 0 , 210
DrawText " ="+azgf_form[2].x , 0 , 220
DrawText "y3=y1+y/DIST*100=" , 0 , 230
DrawText " ="+azgf_form[2].y , 0 , 240
Else
DrawText "DIV TO 0" , 0 , 210
End If
SetLineWidth 5
SetColor 255,255,0
DrawLine azgf_form[0].x,azgf_form[0].y,azgf_form[2].x,azgf_form[2].y
SetLineWidth 1
Case 2
DrawText "x1="+azgf_form[0].x , 0 , 120
DrawText "y1="+azgf_form[0].y , 0 , 130
DrawText "x2="+azgf_form[1].x , 0 , 140
DrawText "y2="+azgf_form[1].y , 0 , 150
DrawText "x3="+azgf_form[2].x , 0 , 160
DrawText "y3="+azgf_form[2].y , 0 , 170
_x1=azgf_form[1].x-azgf_form[0].x
DrawText "x=x2-x1="+_x1 , 0 , 180
_y1=azgf_form[1].y-azgf_form[0].y
DrawText "y=y2-y1="+_y1 , 0 , 190
_x2=azgf_form[0].x-azgf_form[2].x
DrawText "vx=x3-x1="+_x2 , 0 , 200
_y2=azgf_form[0].y-azgf_form[2].y
DrawText "vy=y3-y1="+_y2 , 0 , 210
_xxpyy=Float(_x2*_x1+_y2*_y1)/Float(_x1*_x1+_y1*_y1)
DrawText "coefficient=" , 0 , 220
DrawText " =(vx*_x+_vy*_y)/" , 0 , 230
DrawText " /(x*_x+_y*_y)=" , 0 , 240
DrawText " =("+_x2+"*"+_x1+"+" , 0 , 250
DrawText " +"+_y2+"*"+_y1+")/" , 0 , 260
DrawText " /("+_x1+"*"+_x1+"+" , 0 , 270
DrawText " +"+_y1+"*"+_y1+")=" , 0 , 280
DrawText " ="+_xxpyy , 0 , 290
DrawText "x4=x1-coeff*x=" , 0 , 300
DrawText " ="+(azgf_form[0].x-_xxpyy*_x1) , 0 , 310
DrawText "y4=y1-coeff*y=" , 0 , 320
DrawText " ="+(azgf_form[0].y-_xxpyy*_y1) , 0 , 330
azgf_form[3].x=azgf_form[0].x-_xxpyy*_x1
azgf_form[3].y=azgf_form[0].y-_xxpyy*_y1
SetLineWidth 3
SetColor 255,0,0
DrawLine azgf_form[0].x,azgf_form[0].y,azgf_form[1].x,azgf_form[1].y
SetColor 0,0,255
DrawLine azgf_form[0].x,azgf_form[0].y,azgf_form[2].x,azgf_form[2].y
SetColor 0,255,0
DrawLine azgf_form[0].x,azgf_form[0].y,azgf_form[3].x,azgf_form[3].y
SetLineWidth 1
End Select
End Function
|