Alignment problems.

Blitz3D Forums/Blitz3D Programming/Alignment problems.

Maximus Primal(Posted 2005) [#1]
I am trying to do a driving game where you race in a walled in track. The track has both hills and long banked corners. The track and walls are seperate 3ds models which are loaded in.

I have got the player's vehicle to follow the hills properly. But when I go round the banked curve the player comes out of it slightly misaligned (as in not looking straight ahead). The vehicle is driveable as normal but just isn't facing the correct direction.

Anyone know what might cause this?

Also I have the the collision for the walls set to 2,1 (polygon,stop) but when the player hits a wall they will either drive straight through it or move over it. Again, any ideas why?

The code as it stands is below if it helps. A couple of notes though:

1) I started with Gravity on, but had to disable it as I could not get the correct alignment when using the linepick command with it on for some reason.

2) The objects are a little large (for ease when modeling them as I can adjust them more accurately) hence the large scale factor.


Any suggestions are really welcomed as this is already proving to be a lot harder than I imagined :(

Max




Stevie G(Posted 2005) [#2]
It may be that your linepick isn't picking anything up .. seems quite short for such a large scale scene... Start the pick above the point and make the range longer ...

LinePick sx, sy-20, 0, 100,0


Maximus Primal(Posted 2005) [#3]
The linepick does pick up the track as needed. The ship hovers as it is meant to so it's that (well not that I can see anyway).

Max


Stevie G(Posted 2005) [#4]
Well there isn't very much too your code and that is the only part doing the alignment so it'd be worth playing with.

Also note that if no enitty is picked then the camera will still be trying to align to the last picked command incorrectly.


Ross C(Posted 2005) [#5]
I could be the align to vector command is causing the sip to be aligned wrongly. It's kind of hard to tell though. I remember writing that snippet of code for you. Hmmm. It should align the ship to the angle of the triangle it picks...


Maximus Primal(Posted 2005) [#6]
Correct it does align to the angle of the track. but on a banked curve it misangles it on the exit (or poss during the curve) I don't know why it does that. I wasn't sure if I had missed something or not...

Max


Ross C(Posted 2005) [#7]
It's hard to tell exactly, without seeing it in action... Definetly a wrong value in that statement somewhere. I'll have a fiddle around with it later :o)


Maximus Primal(Posted 2005) [#8]
The media files should be HERE. At least I hope they are! :) 1.6mb.

Max


Ross C(Posted 2005) [#9]
Ok, i've had a look and it's actually the camera thats getting knocked out of sync. :o)


Ross C(Posted 2005) [#10]
Ok man. I've fixed your problems and added a few more things in. Added in hovering above the track and some better acceleration for the ship. Added in comments too, to explain what i've done. Any questions, just post em back in here :o)

Graphics3D 800,600,16,2
Text 320,200,"Please Wait, Loading Data...."

SetBuffer BackBuffer()


; Setup contants and varibles as used in the driver example and modified for what I think I need.

Const GRAVITY#=-5
Const BODY=1,WALL=2,RACETRACK=3,SHIPPIVOT=4

Global dist_above_track# = 50			;	Distance Above Track That Ship Hovers.
Global hover = 0                    ; hover variable to control the hovering above the track
Global hover_speed = 1              ; speed the craft will hover
Global hover_range = 10             ; hover range of distance
Global dist_away_from_wall# = 50
Global bank_angle# = 0
Global max_speed# = 80				;	Maximum Speed The Ship Can Go.
Global speed# = 0						;	Set Speed To 0 At Start.
Global displayspeed# = 0

; Load required Track & Walls and set them up as needed. This includes linking to any variables.

Global track=LoadMesh ("Media/TestTrack3a.3ds")
Global walls=LoadMesh ("Media/TestTrack3b.3ds")
Global scenery=LoadMesh ("Media/TestTrack3c.3ds")


PositionEntity track,0,0,0
PositionEntity walls,0,0,0
PositionEntity scenery,0,0,0
RotateEntity track,0,0,-90
RotateEntity walls,0,0,-90

EntityType track,RACETRACK
EntityType walls,WALL

EntityPickMode track,2
EntityPickMode walls,2

tex=LoadTexture( "Media/tiles.jpg" )
ScaleTexture tex,50,50
EntityTexture track,tex



; Now do the same for the ship (this replaces the car in the driver example).

Global ship=LoadMesh ("Media/fighter.3ds")
RotateMesh ship,0,180,0 ; ADDED <<<< Rotate the mesh so it's facing the +Z. Makes moving it more
                        ; straightforward. Rotatemesh is perminate, and doesn't reset like rotateentity.
PositionEntity ship,0,20,0
EntityShininess ship,1
EntityType ship,BODY

ScaleEntity track,.05,.05,.05
ScaleEntity walls,.05,.05,.05
ScaleEntity scenery,.05,.05,.05
ScaleEntity ship,.05,.05,.05


; Set up the main light for the scene.

light=CreateLight()
PositionEntity (light,0,10,-10)


; Now get the camera up and running...

Global camera = CreateCamera()
CameraRange camera,1,10000
CameraFogMode camera,1
CameraFogRange camera,100,9000
PositionEntity camera,0,25,70
PointEntity camera,ship

Global cam_piv = CreatePivot() ; create camera pivot
							   ; this camera pivot is where the camera will come to rest when motionless
EntityParent cam_piv, ship ; parent the pivot to the ship
PositionEntity cam_piv,0,325,-470 ; location of the pivot in relation to the ship

Global cam_look_piv = CreatePivot() ;the point the camera will point to.
EntityParent cam_look_piv, ship
PositionEntity cam_look_piv,0,150,200

Global cam_speed = 10 ;the speed the camera will align to the ship at. Lower values = faster alignment

; Setup collisions

Collisions BODY,RACETRACK,2,2
Collisions BODY,WALL,2,1


; Setup other variables

camx#=0				; Camera X Angle.
camy#=0				; Camera Y Angle.
camz#=0				; Camera Z Angle.
pitch#=0			; Camera & Ship Rotation Angle.


; Okay that should be everything up and running. Now it is time to do the main program loop.

While Not KeyHit(1)

	
;move ship
	
If KeyDown(203) 

	camy=camy+1
	TurnEntity ship,0,1,0	

ElseIf KeyDown(205) 

	camy=camy-1
	TurnEntity ship,0,-1,0

EndIf

; These lines i put in below replace the old ones you had. This makes the speeding up and slowing down
; more "real" looking. It does this by using the SIN wave. /-\_/ The closer the speed variable
; gets to 90, the exponetially faster the speed gets. It simulates acceleration quite well.

If KeyDown(200)
	speed = speed + 0.4
	If speed > max_speed Then
		speed = max_speed
	End If
ElseIf KeyDown(208) Then
	speed = speed - 0.4
	If speed < 0 Then
		speed = 0
	End If
Else
	speed = speed - 0.2
	If speed < 0 Then
		speed = 0
	End If
End If



sx#=EntityX(ship,True)
sy#=EntityY(ship,True)
sz#=EntityZ(Ship,True)

LinePick sx,sy,sz,0,-100,0

hover = hover + hover_speed


If PickedEntity()<>0
		AlignToVector ship,PickedNX(),PickedNY(),PickedNZ(),2,0.2
		; the same idea as the speed, using the sin wave to hover the ship. Perfect for this type
		; of thing, as all you do is increase the hover variable by a constant number (hover_range)
		; to get the ship to simulate a hovering motion. No need to reset the variable, as this is
		; not needed.
		PositionEntity ship, sx, PickedY() + dist_above_track + (Sin(hover)*hover_range), sz,True

EndIf

	; As explained above, the movement uses the SIN wave. You basically move the ship using the
	; SIN of the speed variable. Because sin values only range from -1 to 1, you need to multiply
	; the result by a number to obtain faster speeds

	MoveEntity ship,0,0,Sin(speed)*20
	displayspeed = speed


	; perform smooth camera operations
	; these code lines point the camera at the cam_pivot, then moves the camera towards that point,
	; based on the distance remaining between them. It gives a smooth movement because the distance
	; is decreasing as the camera moves, and thus, the speed it moves at decreases the closer it gets
	; to the point. The cam_look_piv, is basically just a pivot the camera points to, instead of pointing
	; at the ship. Gives a beter view of the track. Both pivots are parented to the ship, so they move
	; along with it
	PointEntity camera, cam_piv
	MoveEntity camera,0,0,EntityDistance(cam_piv,camera)/ cam_speed
	PointEntity camera, cam_look_piv

	
	UpdateWorld
	RenderWorld
	
	Text 320,500,"Racer V0.01"
	Text 320,520,"Speed:"
	Text 380,520,displayspeed
	Flip

Wend
End



Maximus Primal(Posted 2005) [#11]
Wow! Thank you so much!!!! I didn't expect you to do that. I only expected to be told where to the problem was (and perhaps how to fix it) not to have it fixed for me!!!

That is VERY kind of you. Thank you again!!!

Max


Ross C(Posted 2005) [#12]
No worries ^_^


Stevie G(Posted 2005) [#13]

Also note that if no enitty is picked then the camera will still be trying to align to the last picked command incorrectly.




Ross C(Posted 2005) [#14]
Sorry Stevie, never read that part of your post :S


Maximus Primal(Posted 2005) [#15]
Sorry, got a small question sort of related to this. On the sine wave that gives the ship a more "floaty" feel. How would I speed it up so it does it twice as fast?

Max


Ross C(Posted 2005) [#16]
Increase the "hover_speed" variable :o)