For an angle on the position, you want to map it to radians in some way. One way is using LERP. You may want to print out some values to see how this works.
This example just shows the visual result of the angle itself given the range -1 to 1. These vectors in actuality will be emitting from the point of contact on the bat.
A lot of this is worked out around the unit circle that Godot uses. Where the Y axis heading down is 0, rotating along the right heading upwards ends on PI, and rotating along the left side goes to -PI. (It's worth exploring with visuals and print statements, it comes up a lot.)
I wrote a helper function to do the lerp, while clamping the input to -1 and 1, as well as giving a direction option. It can reflect the other way basically by multiplying a -1.
extends Node2D
const MAX_ANGLE = 45
const LEFT = -1
const RIGHT = 1
const DISTANCE = 100
const VEC_ZERO = Vector2()
onready var canvas_center = get_viewport().get_rect().size / 2
var color = Color(1,1,1,1)
var hit_angle_color = Color(1,0,0,1)
var center_angle = PI / 2 # 90 degrees ------>
var upper_angle = deg2rad(MAX_ANGLE/2 + 90)
func map_value_to_angle(value, direction):
return lerp(center_angle, upper_angle, clamp(value, -1, 1)) * direction
func _draw():
draw_circle(canvas_center, 5, color)
var hit_angle_max = map_value_to_angle( 1, RIGHT)
var hit_angle_75 = map_value_to_angle( .5, RIGHT)
var hit_angle_mid = map_value_to_angle( 0, RIGHT)
var hit_angle_25 = map_value_to_angle(-.5, RIGHT)
var hit_angle_min = map_value_to_angle( -1, RIGHT)
draw_line(canvas_center, canvas_center + Vector2(sin(hit_angle_max), cos(hit_angle_max)) * DISTANCE, hit_angle_color, 2)
draw_line(canvas_center, canvas_center + Vector2(sin(hit_angle_75 ), cos(hit_angle_75 )) * DISTANCE, hit_angle_color, 2)
draw_line(canvas_center, canvas_center + Vector2(sin(hit_angle_mid), cos(hit_angle_mid)) * DISTANCE, hit_angle_color, 2)
draw_line(canvas_center, canvas_center + Vector2(sin(hit_angle_25 ), cos(hit_angle_25 )) * DISTANCE, hit_angle_color, 2)
draw_line(canvas_center, canvas_center + Vector2(sin(hit_angle_min), cos(hit_angle_min)) * DISTANCE, hit_angle_color, 2)