add camera zoom lookahead

This commit is contained in:
interfisch 2024-11-10 01:06:25 +01:00
parent 3c66799413
commit b1789e7e19
3 changed files with 64 additions and 68 deletions

View file

@ -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)

View file

@ -9,6 +9,10 @@ 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

View file

@ -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,6 +71,29 @@ func _process(delta: float) -> void:
if !countdown.is_stopped():
countdown_label.text=str(round(countdown.time_left))
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)
$hud/timer.text=str(round(Gamestate.getTimeElapsed()*1000)/1000.0)
if game_ended:
var anyplayerkeypressed=false
var id=0
for p in Gamestate.getPlayers():
if Input.is_action_pressed(Gamestate.userinput_prefix+str(id)):
anyplayerkeypressed=true
id+=1
if anyplayerkeypressed and timer_close.time_left<time_close_keypressed:
timer_close.wait_time=time_close_keypressed #set time back
timer_close.stop() #prepare for restarting at this time
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
@ -84,18 +108,16 @@ func _process(delta: float) -> void:
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.x=carpos.x
minPos.y=carpos.y
maxPos.x=carpos.x
maxPos.y=carpos.y
minPos=carpos.min(carposLookahead)
maxPos=carpos.max(carposLookahead)
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)
minPos=minPos.min(carpos).min(carposLookahead)
maxPos=maxPos.max(carpos).max(carposLookahead)
displayedCarCount+=1
@ -118,32 +140,11 @@ func _process(delta: float) -> void:
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)
#$hud/debuglabel.text=""+str(calculatedViewCarMargin)+" / "+str(viewCarMargin_zoomstart)+" zoomspeed="+str(mapped_zoomspeed)
#$hud/debuglabel.text=""+str(calculatedViewCarMargin)+" / "+str(viewsize)+" zoomspeed="+str(mapped_zoomspeed)
$hud/timer.text=str(round(Gamestate.getTimeElapsed()*1000)/1000.0)
if game_ended:
var anyplayerkeypressed=false
var id=0
for p in Gamestate.getPlayers():
if Input.is_action_pressed(Gamestate.userinput_prefix+str(id)):
anyplayerkeypressed=true
id+=1
if anyplayerkeypressed and timer_close.time_left<time_close_keypressed:
timer_close.wait_time=time_close_keypressed #set time back
timer_close.stop() #prepare for restarting at this time
if !anyplayerkeypressed and timer_close.is_stopped():
timer_close.start() #start timer when no key is pressed
func _input(ev):
#if ev is InputEventKey and
if Input.is_action_just_pressed("ui_cancel"):