Godot Version
3x, 4x
Question
Hi all! I’m making a 2.5D golf game (like Golf Grand Slam on NES). And I’ve found a great starting point, this project–Fake 2D Z-Axis–which generates sprite2Ds with proper physics, shadows, bouncing, etc. while making use of y-sort to manage the ‘fake z-axis’ effect (you can see a video here).
I’ve reworked the project to generate just one golf ball with a shadow and it’s exactly what I want except for the targeting. While the original project just generates objects and randomizes the x, y, and z velocities, I need to be able to click on the screen and have the equations to calculate the velocities for that destination. After some research, I’m still confused. I know the formula ‘velocity = distance left / time left’ but I’m confused about how to account for the z-axis (or how to implement ‘distance left/time left’ into the current project).
Here’s the current code in the project for producing x, y, and z velocities (I’ve removed the elements from the script that I’m not using but here’s the original). Any ideas on how I might rework this so that the x, y, and z velocities are properly calculated based on a fixed starting position (ballPos) and a destination of get_global_mouse_position()? Any help greatly appreciated!
func _ready():
MainScript.counter += 1
shadowParent = get_node("/root/Main/ShadowViewport")
shadowParent.add_child(shadowInstance)
shadowInstance.set_position(Vector2(x + get_position().x, get_position().y))
y = get_position().y
actual = $Actual
#Need a way to calculate these numbers based on where the player targets
randomize()
xspd = rand_range(-20,20)
yspd = rand_range(-20,20)
zspd = floor(rand_range(-6,-5))
pass
func _physics_process(delta):
zspd += grav * delta
if z + zspd > floorHeight:
if canBounce:
xspd = xspd * 0.7 if xspd > 0.1 else 0
yspd = yspd * 0.7 if yspd > 0.1 else 0
zspd = zspd * -0.4 if zspd > 0.2 else 0
z = zspd if zspd > 0.2 else floorHeight
else:
zspd = 0
xspd = 0
yspd = 0
z = floorHeight
z += zspd
x += xspd * delta
y += yspd * delta
set_position(Vector2(get_position().x,y))
actual.position.x = x
actual.position.y = z
shadowInstance.position.x = x + get_position().x
shadowInstance.position.y = get_position().y