# Predicting a ball's future position by considering bouncing

4

### Question

I want to predict a ball’s position by considering bouncing but it doesn’t work. Here is my all code and ball’s inspector configuration:

``````extends RigidBody2D
# Radius of the bot's collision = 24.69
# Radius of the ball's collision = 17.06
var AOPPSWS=111.06275 # Amount Of Pixels Per Second Walk Speed - En un segundo avanza 111.06275
@export var walkSpeed=int()
@export var turboSpeed=int()
@export var hyperTurboSpeed=float()
@export var turboRemaining=float()
var direction=Vector2()
var velocity=Vector2()
var BTBV #Bot to Ball Vector
@export var MDTT=int() #La distancia máxima entre el bot y la pelota para usar el Turbo-- Max Distance To Turbo
@export var mDTT=int() #La distancia minima entre el bot y la pelota para usar el Turbo-- Min Distance To Turbo
@export var MDTHT=int() #La distancia máxima entre el bot y la pelota para usar el HyperTurbo-- Max Distance To HyperTurbo
@export var player: RigidBody2D
@export var ball: RigidBody2D
var ballFP=Vector2(0,0) # Ball Future Position
@export var upCorner: Node2D
@export var downCorner: Node2D
@export var DTBBS=float() #Distance To Ball Before Shot
@export var timePrediction: Array=[
0.6, 0.7, 0.8, 0.9, 1.0,
1.1, 1.2, 1.3, 1.4, 1.5,
1.6, 1.7, 1.8, 1.9, 2.0]

var move=true
#Obtiene la magnitud de un vector dado
func GetMagnitude(vector):
var distance=sqrt(vector.x**2+vector.y**2)
return distance

func hypotenuseNormalized(vector):
var hyp=sqrt(vector.x**2+vector.y**2)
var vectorNormalized=vector/hyp
return vectorNormalized

func FuturePositionBounce():
var collisionPoint=ball.ball_ray_cast.get_collision_point()
# Collision Point To Ball Position Magnitude
var CPTBPM = GetMagnitude(ball.position-collisionPoint)
# The remaining part of the raycast after the wall
var magnitudeRemaining=GetMagnitude(ball.ball_ray_cast.target_position)-CPTBPM
var wallNormal= ball.ball_ray_cast.get_collision_normal()
var vector=Vector2(cos(angle),sin(angle))
var unitVector=hypotenuseNormalized(vector)
var localballFP=collisionPoint+unitVector*magnitudeRemaining
#	player.position=localballFP
return localballFP
func GetAngle(CPTBM,wallNormal):
if wallNormal.x==1:
var x = GetMagnitude(ball.left_ray_cast.get_collision_point()-ball.position)
var angle = atan2(CPTBM,x)
return angle
elif wallNormal.x==-1:
var x = GetMagnitude(ball.right_ray_cast.get_collision_point()-ball.position)
var angle = atan2(CPTBM,x)
return angle
elif wallNormal.y==1:
var x = GetMagnitude(ball.up_ray_cast.get_collision_point()-ball.position)
var angle = atan2(CPTBM,x)
return angle
elif wallNormal.y==-1:
var x = GetMagnitude(ball.down_ray_cast.get_collision_point()-ball.position)
var angle = atan2(CPTBM,x)
return angle

pass
func _physics_process(delta):
if Input.is_action_just_pressed("ui_focus_next"):
position=Vector2(893.952,364.697)
if Input.is_action_just_pressed("ui_accept"):
# ballFP= ball future position
for time in timePrediction:
ballFP=ball.position+(ball.linear_velocity*time)
ball.ball_ray_cast.target_position=ballFP-ball.position
await get_tree().create_timer(0.0000000001).timeout
if ball.ball_ray_cast.is_colliding():
if ball.ball_ray_cast.get_collider().is_in_group("Player"):
break
elif ball.ball_ray_cast.get_collider().get_parent().name=="WallContainer":
FuturePositionBounce()
# Down Corner To Ball Future Position Vector
var DCTBFPV=ballFP-downCorner.position
# Up Coner To Ball Future Position Vector
var UCTBFPV=ballFP-upCorner.position
var maxAngleToShot=hypotenuseNormalized(DCTBFPV)
var minAngleToShot=hypotenuseNormalized(UCTBFPV)
var randomAngleToShotX=randf_range(minAngleToShot.x,maxAngleToShot.x)
var randomAngleToShotY=randf_range(minAngleToShot.y,maxAngleToShot.y)
var randomAngleToShot=Vector2(randomAngleToShotX,randomAngleToShotY)
var positionToShot=ballFP+randomAngleToShot*DTBBS # Distance To Ball Before Shot
if GetMagnitude(positionToShot-position)<AOPPSWS*time*0.76:
while positionToShot.distance_to(position)>2:
var direction=hypotenuseNormalized(positionToShot-position)
apply_central_force(walkSpeed*direction)
await get_tree().create_timer(get_physics_process_delta_time()).timeout
linear_velocity=Vector2(0,0)
position=positionToShot
while true:
if GetMagnitude(ball.position-ballFP)/GetMagnitude(ball.linear_velocity)<=0.51:
var timer=0.3
while timer>0:
apply_central_force(hyperTurboSpeed*hypotenuseNormalized(ballFP-position))
timer-=get_physics_process_delta_time()
await get_tree().create_timer(get_physics_process_delta_time()).timeout
#						apply_central_impulse(walkSpeed*hypotenuseNormalized(ballFP-position))
break
await get_tree().create_timer(0.00001).timeout
break

if move==true:
if get_parent().get_node("Ball")!=null:
BTBV=ball.global_position-global_position
# Soluciona que el bot atrape la pelota contra la pared
#Si su velocidad es poca (Porque su velocidad es mínima al encerrar a la pelota)
if GetMagnitude(linear_velocity)<3.1:
#Mientras su distancia contra la pelota no sea mayor a 100 px
while GetMagnitude(BTBV)<100:
#Se mueve para el lado opuesto con respecto BTBV
apply_central_force(-velocity)
#Para que no deje de responder se espera un poco
await get_tree().create_timer(0.01).timeout
``````