Skidmark Scatter Aches

Blitz3D Forums/Blitz3D Programming/Skidmark Scatter Aches

Hey all,

FIRST: Crap I posted in the WRONG forum, lol, meant to go in BLITZ3D Programming. Sorry


Tinkering with some code, after butchering other code snipets I found post around here, and got a few questions to the smarter than the rest of us Guru's.

1 - Trying to find a way to make the next created quad start at the last created wuads vert position, in that they are all linked and not spaced out as seperate objects.
2 - If this method is functional enuff to be worth using.
3 - I created a poor mans method of trying to make the next created quad semi match the turn angle of the last, but because they "overlap" if you were to alpha them for better FX then you get that Overlayed darkening of any two quads made next to eachother. I assuming that a fix for problem #1 would resolve this.

Here's the Media used:


And the following Code:
Graphics3D 800,600,0,2
SetBuffer BackBuffer()

AppTitle "Skidmark Testing"

Global Speed# 		= 0
Global Yaw#		 = 0
Global TurnAngle#     = 0
Global Move#, Turn#

ClearTextureFilters()

; ##################################################################################

Camera = CreateCamera()
	CameraClsColor Camera,130,130,130

; ##################################################################################

Light  = CreateLight()
	AmbientLight 130,130,130
		
; ##################################################################################

Grid   = CreateTexture(64,64,1+8)
	
	SetBuffer TextureBuffer(grid)
	Color 255,255,255
	Rect  0  ,0  ,64,64
	Color 255,0  ,0
	Rect  0  ,0  ,64,64,False
	ScaleTexture grid,8,8
	SetBuffer BackBuffer()

; ##################################################################################

Floor = CreatePlane()
		EntityTexture Floor,grid
		MoveEntity    Floor,0,0,0

; ##################################################################################

Global TrailTex = LoadTexture("SkidDirt.png",4)

; ##################################################################################

Global DummyObject = CreateCube()
	
		ScaleEntity DummyObject,2,2,4
		EntityColor DummyObject,100,100,250

Timer = CreateTimer(60)

; ##################################################################################
; #					Main Loop													   #
; ##################################################################################

While Not KeyHit(1)

	WaitTimer(Timer)

	UpdateKeys()
	Update_Camera(DummyObject,camera,10,5)
	
	Free_Skidmarks()

	RotateEntity DummyObject,0,turn,0
	MoveEntity   DummyObject,0,0,move


	If KeyHit(17) Then wframe = 1 - wframe
		WireFrame wframe

	UpdateWorld
	RenderWorld
	
	Text 0,10,"Tri's     :"+TrisRendered()
	Text 0,20,"Speed#    :"+Move#
	
	Flip

Wend
End

; ##################################################################################

	Type trail

		Field Mesh
		Field Surface
		Field FadeTime#
		Field Texture
	
	End Type

; ##################################################################################


Function CreateSkid(segs#=2,parent=0,FadeTime#,Texture)

	Skidmark.trail = New Trail

		Skidmark\mesh      = CreateMesh(parent)
		Skidmark\surface   = CreateSurface(Skidmark\mesh)
		Skidmark\FadeTime# = FadeTime#
		Skidmark\Texture   = Texture
		
	    l#  = -.5
    	b#  = -.5
	    tvc =   1

    ;create all the vertices first
    Repeat

      u#  = l   + .5
      v#  = b   + .5
	      AddVertex Skidmark\surface,l,0,b,u,1-v
      tvc = tvc +  1
      l   = l   +  1 / segs

      If l > .501 Then
        l = -.5
        b = b + 1/segs
      End If
    Until b > .5

    ;create polys
    vc# = 0

    Repeat

      AddTriangle (Skidmark\surface,vc,vc+segs+1,vc+segs+2)
      AddTriangle (Skidmark\surface,vc,vc+segs+2,vc+1)

      vc = vc + 1
      tst# =  ((vc+1) /(segs+1)) -Floor ((vc+1) /(segs+1))

      If (vc > 0) And (tst=0) Then
        vc = vc + 1
      End If

    Until vc => tvc - segs - 2
 
    UpdateNormals Skidmark\mesh
	EntityTexture Skidmark\mesh,Texture

    Return Skidmark\mesh

End Function


; ##################################################################################


Function Free_Skidmarks()

	For Skidmark.trail = Each trail

		Skidmark\FadeTime# = Skidmark\FadeTime# - 0.005

		If Skidmark\FadeTime# < 0
			FreeEntity Skidmark\mesh
			Delete skidmark
		EndIf
	Next

End Function



; ##################################################################################

Function Update_Camera(Character,Camera,Distance#,Hheight#,Angle=180)

	;get player values
	px#=EntityX(character)
	py#=EntityY(character)
	pz#=EntityZ(character)
	
	pa#=EntityYaw(character)+angle
	
	;get camera values
	cx#=EntityX(camera)
	cy#=EntityY(camera)
	cz#=EntityZ(camera)
	
	;work out new values
	cx = curvevalue(cx,newxvalue(px,pa,distance),10)
	cy = curvevalue(cy,py+height,7)
	cz = curvevalue(cz,newzvalue(pz,pa,distance),10)

	;update camera position

	PositionEntity camera,cx,cy,cz
	PointEntity camera,character
	
	;EntityType camera,c_camera
	
End Function


; ##################################################################################

Function CurveValue#(current#,destination#,amount)
	current = current + ((destination - current) / amount)
		If current < 0.01 And current > - 0.01 Then current = 0
	Return current
End Function

Function NewXValue#(current#,ang#,dist#)
	Return current-Sin(ang)*dist
End Function

Function NewYValue#(current#,ang#,dist#)
	Return current+Sin(ang)*dist
End Function

Function NewZValue#(current#,ang#,dist#)
	Return current+Cos(ang)*dist
End Function


; ##################################################################################

Function UpdateKeys()

;##############################
;### Movement Keys          ###
;##############################

	If KeyDown(200) 
		move# = move# + 0.1
	Else
		Move# = move# - 0.01
	EndIf

	If KeyDown(208) 
		move# = move# - 0.1
	EndIf
	
	If KeyDown(203) 
		turn# = turn# + 8	
		Turnangle# = 2.5
	EndIf
		
	If KeyDown(205) 
		turn# = turn# - 8
		Turnangle# = -2.5
	EndIf

	If KeyDown(203) = 0 And KeyDown(205) = 0
		Turnangle# = 0.0
	EndIf
	
	If Move# >  5 Then Move# = 5
	If Move# <= 0 Then Move# = 0.0
	
;########################################
;### Hit Left Space to Leave Skidmark ###
;########################################

	If KeyDown(57) And move# > 0.5

		box = CreateSkid(1,0,1,TrailTex)
		ScaleEntity    box,2,2,2
		
		Yaw# = EntityYaw#(DummyObject)
		
		PositionEntity box,EntityX(DummyObject), .1, EntityZ(DummyObject)
		RotateEntity   box,0,Yaw# + Turnangle#,0
		EntityFX       box,2
		
	EndIf
	
End Function


Any input or corrections would be greatly welcomed and a relief.

Topknot ;)

Check the tron demo in samples/mak for an example of creating such a mesh on the fly.

Thanks,

Now here's a greatly chopped down code, only major problem is I can't get the vert alignment to be flat in line with the ground. If you run the code you'll notice it twist as you turn. It's late and I'm sure I'm missing a very noticable item. Anyone care to point it out to me.

Or at least describe how to make a quad that's laying flat on the ground plane, meaning vert creation order. ;)

Updated Below


Hitting W will show the mesh creation problems

Topknot ;)

Well all was hoping for at least One clue on how to turn this mesh trail flat, are the forums really that dead...?

I did manage to add in a feature where you hit space and a Single flat quad is made, but after several hours I still can't see where to change the mesh trail following the box on it's side. The vert's seems right being created at the X & Z points.

Updated Code:
Graphics3D 800,600,0,2

grid_tex = CreateTexture( 32,32,8 )
	ScaleTexture grid_tex,10,10
	SetBuffer TextureBuffer( grid_tex )
	Color 0,0,64  : Rect 0,0,32,32
	Color 0,0,255 : Rect 0,0,32,32,False
	SetBuffer BackBuffer()

grid_plane = CreatePlane()
	EntityTexture grid_plane,grid_tex
	EntityBlend   grid_plane,1

Global bike = CreateCube()
	ScaleMesh       bike,.75,1,2
	PositionEntity  bike,0,1,0
	EntityColor     bike,200,100,0

camera = CreateCamera()
	TurnEntity camera,45,0,0

light = CreateLight()
	TurnEntity light,45,45,0


; ## Create Start Point #

		trail_mesh  = CreateMesh()
		trail_surf  = CreateSurface(trail_mesh)

		AddVertex (trail_surf, 0, 0, -1,0,0)
		AddVertex (trail_surf, 0, 0,  0,0,0)
		AddVertex (trail_surf, 1, 0,  0,0,1)
		AddVertex (trail_surf, 1, 0, -1,0,1)
		

	
While Not KeyHit(1)

	If KeyHit(17) Then wframe = 1 - wframe
		WireFrame wframe
	
	If KeyHit(57) Then
		MakeQuad()
	EndIf
	
		turn = 0
		move = 0.0
		
		If KeyDown(200) move = 0.6
		If KeyDown(208) move = -.5


		If KeyDown(203) turn =  2
		If KeyDown(205) turn = -2

			
		TurnEntity bike,0,turn,0
		MoveEntity bike,0,0,move
	


		If move > 0
		
		AddVertex trail_surf,EntityX(bike), EntityY(bike),   EntityZ(bike),0,0
		AddVertex trail_surf,EntityX(bike),         0,       EntityZ(bike),0,1

		AddTriangle trail_surf,trail_vert+2,trail_vert,trail_vert+3
		AddTriangle trail_surf,trail_vert+3,trail_vert,trail_vert+1
		AddTriangle trail_surf,trail_vert+3,trail_vert,trail_vert+2
		AddTriangle trail_surf,trail_vert+1,trail_vert,trail_vert+3


		trail_vert = trail_vert + 2
		
	
		EndIf
		
		
	UpdateWorld
	
	PositionEntity camera,EntityX(bike),20,EntityZ(bike)-20

	RenderWorld
	
	Text 0,10,"Tris :"+TrisRendered()
	
	Flip

Wend
End

Function MakeQuad()

		mesh     = CreateMesh()
		surface  = CreateSurface(mesh)

		v0 = AddVertex (surface, 0, 0, -1,    0,0)
		v1 = AddVertex (surface, 0, 0,  0,     0,0)
		v2 = AddVertex (surface, 1, 0,  0,     0,1)
		v3 = AddVertex (surface, 1, 0, -1,     0,1)

		tri1 = AddTriangle(surface,v0,v1,v2)
		tri2 = AddTriangle(surface,v0,v2,v3)
		
		UpdateNormals mesh
		ScaleMesh     mesh,2,2,2
		PositionMesh  mesh,EntityX(bike),4,EntityZ(bike)

		
End Function


Just one pointer to assist would be most welcomed.

Topknot ;)

just having a play, i replaced the two addvertex calls at line 65 with this:
		TFormPoint -10,0,0,bike,0
		AddVertex trail_surf,TFormedX(),TFormedY(),TFormedZ(),0,0
		TFormPoint 10,0,0,bike,0
		AddVertex trail_surf,TFormedX(),TFormedY(),TFormedZ(),0,0


the tformpoint translates a point on each side of the bike into global coordinates, make the 10's smaller to match the width of your vehicle

for texturing you will need to consider how to set the uv texturing coordinates, the u values will be 0 and 1 the v coordinates should increment the distance the vehicle has traveled since the last time you added to the skidmesh and your skid texture will need to be set to wrap so it repeats...

Like this ...

		If move > 0
			TFormPoint 1,0,0, Bike, 0
			AddVertex trail_surf, TFormedX(), .1, TFormedZ()
			TFormPoint -1,0,0, Bike, 0
			AddVertex trail_surf, TFormedX(), .1 , TFormedZ() 
			AddTriangle trail_surf,trail_vert+2,trail_vert,trail_vert+3
			AddTriangle trail_surf,trail_vert+3,trail_vert,trail_vert+1
			AddTriangle trail_surf,trail_vert+3,trail_vert,trail_vert+2
			AddTriangle trail_surf,trail_vert+1,trail_vert,trail_vert+3
			trail_vert = trail_vert + 2
		EndIf


EDIT ... just like skidracer!!

I'm creating skidmarks on the fly in StuntCarRacing - all I've done is create 2 pivots for the inside and outside tyre contact point for each wheel, then when a skidmark is required I just create a new poly between the pivot positions and the last 2 verts used. These new 2 verts' positions are then stored to be used as the last 2 verts for the next new poly.

I also use a flagging system so that if the skidmark stops, a flag is set to prevent the 1st poly being created when a new skid is started. This prevents the sudden appearance of a huge stretched poly across the world and means the new skidmark starts neatly.

Using pivots as child objects to the vehicle bodymesh means that the skidmarks are always correctly aligned to the ground the car is sitting on, so long as the body is aligned correctly. Ideally I would parent the pivots to the wheel, but this doesn't work if the wheel rotates and thus requires the wheel to be a child object to a wheel-centre pivot, and the skidmarks pivots' would be children of this wheel-centre pivot also. Then you'd rotate the wheel-centre pivot along the steering axis to turn the wheels left and right, which would also move the skidmark pivots, and then spin just the wheel child object around the axle axis to make the wheels visually spin.

Thank you all extremely much, It's more than I hoped for:

@ skid & Steve, May a 1000 Vestal Virgins take comfort in your touch ;) Finally the blasted mesh is flat ;)

@ Vorderman, the part of storing the verts is very interesting, no chance of sharing your method or a faxsimly of such a method? The whole pivot/child thingy is a breeze but the storing of the verts is what would finish this off, cause like you said it creates a mesh between the last places verts and the newly created puppies ;)

Again thank you all....if you knew the frsutration this has caused me in the last several hours, and many attempts, not to menthod a pack of cigs used up in the process.

Topknot ;)