add camera zoom lookahead
This commit is contained in:
parent
3c66799413
commit
b1789e7e19
3 changed files with 64 additions and 68 deletions
|
@ -156,16 +156,6 @@ func get_input():
|
|||
else:
|
||||
steer_direction=0 #drive straight
|
||||
|
||||
# Manual steering here
|
||||
'''
|
||||
var turn = 0
|
||||
if Input.is_action_pressed("ui_right"):
|
||||
turn += 1
|
||||
if Input.is_action_pressed("ui_left"):
|
||||
turn -= 1
|
||||
if turn!=0:
|
||||
steer_direction = turn*deg_to_rad(steering_angle)
|
||||
'''
|
||||
|
||||
|
||||
if running:
|
||||
|
@ -218,9 +208,10 @@ func calculate_steering(delta:float):
|
|||
|
||||
func getRound():
|
||||
var i=getNextCPindex()/checkpoints.size()
|
||||
print("Round="+str(i)+" cpindex="+str(getNextCPindex())+" size="+str(checkpoints.size()))
|
||||
return i
|
||||
|
||||
|
||||
|
||||
func check_markers():
|
||||
if ray_cast_car.is_colliding():
|
||||
#print("Marker: "+str(ray_cast_car.get_collider()))
|
||||
|
@ -244,8 +235,8 @@ func check_markers():
|
|||
if checkpoint_i>=0 and nextcp_i>=0: #found and there is a next checkpoint time free
|
||||
if (nextcp_i%checkpoints.size())==checkpoint_i: #this cp is next cp
|
||||
checkpointtimes[nextcp_i]=Gamestate.getTimeElapsed()
|
||||
print("Player "+str(playerid)+" Checkpoint "+str(ray_cast_car.get_collider().name))
|
||||
print("New CP array "+str(checkpointtimes))
|
||||
#print("Player "+str(playerid)+" Checkpoint "+str(ray_cast_car.get_collider().name))
|
||||
#print("New CP array "+str(checkpointtimes))
|
||||
|
||||
func constrain(val,a,b):
|
||||
var vmin=min(a,b)
|
||||
|
|
|
@ -8,7 +8,11 @@ func getSpeed() -> float:
|
|||
|
||||
func getPosition():
|
||||
return cbcar.position
|
||||
|
||||
|
||||
func getPositionLookahead(t:float):
|
||||
#return position of car in t seconds
|
||||
return getPosition()+cbcar.velocity*t
|
||||
|
||||
func setPosition(p:Vector2):
|
||||
cbcar.position=p
|
||||
|
||||
|
|
109
scripts/game.gd
109
scripts/game.gd
|
@ -14,13 +14,14 @@ extends Node2D
|
|||
|
||||
const caroffset= 32+4 #space cars on start line
|
||||
|
||||
var viewCarMargin=Vector2(0.6,0.6) #1=zoom out full speed when car is at border. 0.9 zoom out full speed when car is 10% away from border
|
||||
var viewCarMargin=Vector2(0.7,0.7) #1=zoom out full speed when car is at border. 0.9 zoom out full speed when car is 10% away from border
|
||||
var viewCarMargin_zoomstart=viewCarMargin-Vector2(0.2,0.2) #start zooming
|
||||
var viewCarMargin_zoombackup=viewCarMargin_zoomstart-Vector2(0.1,0.1) #start zooming back in
|
||||
var zoomspeed=0.5
|
||||
var zoomspeed_backup=0.1 #relative to screen size
|
||||
var zoom_normal=1.5
|
||||
const CAMERA_POSITION_SPEED=0.02 #0.0 - 1.0, higher=faster
|
||||
var camera_zoom_out_lookahead_time=1 #predicted position of car in t seconds for camera coverage. greater value=earlier zoom out and movement of camera
|
||||
|
||||
var running=false
|
||||
|
||||
|
@ -70,59 +71,7 @@ func _process(delta: float) -> void:
|
|||
if !countdown.is_stopped():
|
||||
countdown_label.text=str(round(countdown.time_left))
|
||||
|
||||
var cars=cars.get_children()
|
||||
var displayedCarCount=0
|
||||
var maxCarSpeed=0
|
||||
var minPos=Vector2.ZERO #min/max x and y position of all cars
|
||||
var maxPos=Vector2.ZERO
|
||||
|
||||
var oneDriving=false
|
||||
for c in cars: #check if any one car is still driving
|
||||
if !c.hasFinished():
|
||||
oneDriving=true
|
||||
|
||||
for c in cars:
|
||||
if !c.hasFinished() or !oneDriving:
|
||||
var carpos = c.getPosition()
|
||||
|
||||
maxCarSpeed=max(maxCarSpeed,c.getSpeed())
|
||||
if displayedCarCount==0:
|
||||
minPos.x=carpos.x
|
||||
minPos.y=carpos.y
|
||||
maxPos.x=carpos.x
|
||||
maxPos.y=carpos.y
|
||||
else:
|
||||
minPos.x=min(minPos.x,carpos.x)
|
||||
minPos.y=min(minPos.y,carpos.y)
|
||||
maxPos.x=max(maxPos.x,carpos.x)
|
||||
maxPos.y=max(maxPos.y,carpos.y)
|
||||
|
||||
displayedCarCount+=1
|
||||
|
||||
|
||||
camera.position=lerp(camera.position,(minPos+maxPos)/2.0,CAMERA_POSITION_SPEED)
|
||||
|
||||
|
||||
|
||||
var viewsize = camera.get_viewport_rect().size
|
||||
var carSpread=Vector2(maxPos.x-minPos.x,maxPos.y-minPos.y)
|
||||
var calculatedViewCarMargin=carSpread*camera.zoom #when cars are screen width apart this number matches viewsize=camera.get_viewport_rect().size
|
||||
|
||||
|
||||
var viewCarMargin_zoomstart_pixels=viewCarMargin_zoomstart*viewsize/camera.zoom
|
||||
var viewCarMargin_pixels=viewCarMargin*viewsize/camera.zoom
|
||||
var viewCarMargin_zoombackup_pixels=viewCarMargin_zoombackup*viewsize/camera.zoom
|
||||
|
||||
var mapped_zoomspeed_x=constrain(remap(calculatedViewCarMargin.x , viewCarMargin_zoomstart_pixels.x,viewCarMargin_pixels.x,0,zoomspeed),0,zoomspeed)
|
||||
var mapped_zoomspeed_y=constrain(remap(calculatedViewCarMargin.y , viewCarMargin_zoomstart_pixels.y,viewCarMargin_pixels.y,0,zoomspeed),0,zoomspeed)
|
||||
var mapped_zoomspeed=max(mapped_zoomspeed_x,mapped_zoomspeed_y)
|
||||
if calculatedViewCarMargin.x>viewCarMargin_zoomstart_pixels.x or calculatedViewCarMargin.y>viewCarMargin_zoomstart_pixels.y: #cars not in view
|
||||
camera.zoom-=Vector2(mapped_zoomspeed*delta,mapped_zoomspeed*delta)
|
||||
$hud/debuglabel.set("theme_override_colors/font_color",Color(0,0,0.7))
|
||||
elif calculatedViewCarMargin.x<viewCarMargin_zoombackup_pixels.x and calculatedViewCarMargin.y<viewCarMargin_zoombackup_pixels.y: #cars in view again
|
||||
|
||||
if camera.zoom.x<zoom_normal:
|
||||
camera.zoom+=Vector2(zoomspeed_backup*delta,zoomspeed_backup*delta)
|
||||
updateCameraMovement(delta)
|
||||
|
||||
#$hud/debuglabel.text=""+str(calculatedViewCarMargin)+" / "+str(viewCarMargin_zoomstart)+" zoomspeed="+str(mapped_zoomspeed)
|
||||
#$hud/debuglabel.text=""+str(calculatedViewCarMargin)+" / "+str(viewsize)+" zoomspeed="+str(mapped_zoomspeed)
|
||||
|
@ -143,6 +92,58 @@ func _process(delta: float) -> void:
|
|||
if !anyplayerkeypressed and timer_close.is_stopped():
|
||||
timer_close.start() #start timer when no key is pressed
|
||||
|
||||
func updateCameraMovement(delta: float):
|
||||
|
||||
var cars=cars.get_children()
|
||||
var displayedCarCount=0
|
||||
var maxCarSpeed=0
|
||||
var minPos=Vector2.ZERO #min/max x and y position of all cars
|
||||
var maxPos=Vector2.ZERO
|
||||
|
||||
var oneDriving=false
|
||||
for c in cars: #check if any one car is still driving
|
||||
if !c.hasFinished():
|
||||
oneDriving=true
|
||||
|
||||
for c in cars:
|
||||
if !c.hasFinished() or !oneDriving:
|
||||
var carpos = c.getPosition()
|
||||
var carposLookahead= c.getPositionLookahead(camera_zoom_out_lookahead_time)
|
||||
$hud/debuglabel.text=""+str(carpos)+"\n"+str(carposLookahead)
|
||||
|
||||
maxCarSpeed=max(maxCarSpeed,c.getSpeed())
|
||||
if displayedCarCount==0:
|
||||
minPos=carpos.min(carposLookahead)
|
||||
maxPos=carpos.max(carposLookahead)
|
||||
else:
|
||||
minPos=minPos.min(carpos).min(carposLookahead)
|
||||
maxPos=maxPos.max(carpos).max(carposLookahead)
|
||||
|
||||
displayedCarCount+=1
|
||||
|
||||
|
||||
camera.position=lerp(camera.position,(minPos+maxPos)/2.0,CAMERA_POSITION_SPEED)
|
||||
|
||||
|
||||
|
||||
var viewsize = camera.get_viewport_rect().size
|
||||
var carSpread=Vector2(maxPos.x-minPos.x,maxPos.y-minPos.y)
|
||||
var calculatedViewCarMargin=carSpread*camera.zoom #when cars are screen width apart this number matches viewsize=camera.get_viewport_rect().size
|
||||
|
||||
|
||||
var viewCarMargin_zoomstart_pixels=viewCarMargin_zoomstart*viewsize/camera.zoom
|
||||
var viewCarMargin_pixels=viewCarMargin*viewsize/camera.zoom
|
||||
var viewCarMargin_zoombackup_pixels=viewCarMargin_zoombackup*viewsize/camera.zoom
|
||||
|
||||
var mapped_zoomspeed_x=constrain(remap(calculatedViewCarMargin.x , viewCarMargin_zoomstart_pixels.x,viewCarMargin_pixels.x,0,zoomspeed),0,zoomspeed)
|
||||
var mapped_zoomspeed_y=constrain(remap(calculatedViewCarMargin.y , viewCarMargin_zoomstart_pixels.y,viewCarMargin_pixels.y,0,zoomspeed),0,zoomspeed)
|
||||
var mapped_zoomspeed=max(mapped_zoomspeed_x,mapped_zoomspeed_y)
|
||||
if calculatedViewCarMargin.x>viewCarMargin_zoomstart_pixels.x or calculatedViewCarMargin.y>viewCarMargin_zoomstart_pixels.y: #cars not in view
|
||||
camera.zoom-=Vector2(mapped_zoomspeed*delta,mapped_zoomspeed*delta)
|
||||
elif calculatedViewCarMargin.x<viewCarMargin_zoombackup_pixels.x and calculatedViewCarMargin.y<viewCarMargin_zoombackup_pixels.y: #cars in view again
|
||||
|
||||
if camera.zoom.x<zoom_normal:
|
||||
camera.zoom+=Vector2(zoomspeed_backup*delta,zoomspeed_backup*delta)
|
||||
|
||||
func _input(ev):
|
||||
#if ev is InputEventKey and
|
||||
|
|
Loading…
Reference in a new issue