Показать сообщение отдельно
Старый 20.03.2011, 02:29   #1353
LLI.T.A.L.K.E.R.
Мастер
 
Аватар для LLI.T.A.L.K.E.R.
 
Регистрация: 24.06.2009
Адрес: Набережные Челны
Сообщений: 930
Написано 292 полезных сообщений
(для 504 пользователей)
Ответ: Вопрос-Ответ (для новичков)

Рисунок: http://s53.radikal.ru/i141/1103/92/ea40fd599a7b.jpg

Код внутри

; AddMesh Example 
; --------------- 

Graphics3D 640,480 
SetBuffer BackBuffer() 

type_ground=1 
type_character=2 

Dim temp_vertex(4,4)

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

light=CreateLight() 
RotateEntity light,90,0,0 

; Create tree mesh (upper half) 
tree=CreateMesh() 
PositionMesh tree,0,1.5,0

width#=3
height#=3

For i=1 To 50 Step 10
	For j=1 To 50 Step 10
		Local surface=CreateSurface(tree)
		temp_vertex(0,0)=AddVertex(surface,i-width#,0,j-height#)
		temp_vertex(0,1)=AddVertex(surface,i-width#,0,j+height#)
		temp_vertex(1,0)=AddVertex(surface,i+width#,0,j-height#)
		temp_vertex(1,1)=AddVertex(surface,i+width#,0,j+height#)
		VertexTexCoords surface,temp_vertex(0,0),0,1,0
		VertexTexCoords surface,temp_vertex(0,1),0,0,0
		VertexTexCoords surface,temp_vertex(1,0),1,1,0
		VertexTexCoords surface,temp_vertex(1,1),1,0,0
		AddTriangle(surface,temp_vertex(0,0),temp_vertex(0,1),temp_vertex(1,1))
		AddTriangle(surface,temp_vertex(1,1),temp_vertex(1,0),temp_vertex(0,0))
	Next
Next
UpdateNormals tree
EntityType tree,type_ground 

Player=CreateCube()
PositionEntity player,-7,0,-4
EntityType player,type_character

Collisions type_character,type_ground,2,3

While Not KeyDown( 1 )

MoveEntity player,0.1,0,0.1
PointEntity camera,player

UpdateWorld
RenderWorld 

Draw_Wire_2D(tree,camera,255,0,0)

Flip 

Wend 

End 

Function Draw_Wire_2D(mesh,cam,red=0,grn=0,blu=0)
	Color red,grn,blu
	If EntityInView(mesh,cam)=False Then Return
	cnt=CountSurfaces(mesh)
	For a=1 To cnt
		surf=GetSurface(mesh,a)
		For tri=0 To CountTriangles(surf)-1
			TFormPoint VertexX(surf,TriangleVertex(surf,tri,0)),VertexY(surf,TriangleVertex(surf,tri,0)),VertexZ(surf,TriangleVertex(surf,tri,0)),mesh,0
			CameraProject(cam,TFormedX(),TFormedY(),TFormedZ())
			sx1=ProjectedX()
			sy1=ProjectedY()
			TFormPoint VertexX(surf,TriangleVertex(surf,tri,1)),VertexY(surf,TriangleVertex(surf,tri,1)),VertexZ(surf,TriangleVertex(surf,tri,1)),mesh,0
			CameraProject(cam,TFormedX(),TFormedY(),TFormedZ())
			sx2=ProjectedX()
			sy2=ProjectedY()
			TFormPoint VertexX(surf,TriangleVertex(surf,tri,2)),VertexY(surf,TriangleVertex(surf,tri,2)),VertexZ(surf,TriangleVertex(surf,tri,2)),mesh,0
			CameraProject(cam,TFormedX(),TFormedY(),TFormedZ())
			sx3=ProjectedX()
			sy3=ProjectedY()
			If Check_Cull_2D(sx1,sy1,sx2,sy2,sx3,sy3)<0
				Line sx1,sy1,sx2,sy2
				Line sx2,sy2,sx3,sy3
				Line sx3,sy3,sx1,sy1	
			End If
		Next			
	Next
End Function

Function Check_Cull_2D(x1,y1,x2,y2,x3,y3)
	 Return (x1-x2)*(y3-y2)-(y1-y2)*(x3-x2)
End Function


Почему коллизия не действует на добавленные к мешу трианглы?

Примечание: кубик Player ударяется в стенку в позиции 0,0,0 радиусом 1

Кто знает RMFLoader 0.08 (загрузчик карт редактора Valve Hammer Editor), то там уровень строится по трианглам. И коллизия действует.
Сам сравнить код пока затрудняюсь, так как там он сильно развлетвлён.

Решение - ниже.
(Offline)
 
Ответить с цитированием