0 votes

I'm making a tennis game using a RigidBody2d for the ball and kinematicBody2d for the player. The player can move forwards when the player hits the ball it causes the ball to speed up. What is the code to add to the ball script to set a max speed?

Here is the script for the ball

export var ball_speed = 150                                                                                                      

func _ready():
apply_impulse(Vector2(), Vector2(1, 1).normalized() * ball_speed)
in Engine by (19 points)

1 Answer

+1 vote
Best answer

In your code you apply a one-time force (impulse) to the ball. The speed is just a result from applying the impulse. So you named your variable wrong. How fast the ball will go will also depends on the set mass of the rigid body.

To slow down the ball while it is in the air I would add a "draft" force/impulse in opposite direction of the balls velocity. Probably you will also have a floor so the friction will bring the ball to a stop.

A rough estimation of air draft will be that it is proportional to the square of its speed. So in this code below i put the square of the velocity (length of velocity vector2) in sqrVelocity. Also the applied impulse is multiplied by delta so the draft_factor basically stays for the draft force applied in one second. (depending on velocity)

export var draft_factor = 10.0

func _physics_process(delta):
   sqrVelocity = linear_velocity().length()*linear_velocity().length()
   apply_impulse(Vector2(),linear_velocity().normalized() * sqrVelocity * -1 * delta * draft_factor)

You will have to try out the draft factor.

by (3,246 points)
selected by

Thanks for your response. I tried to add the code but had the following error Identifier 'sqrVelocity' is not declared in current scope. Sorry I'm completely new to GDscript.

I'm making a game like SpiritSphere DX. Using a RigidBody2D for the ball do I need to use integrateforces() instead to access the Physics2DDirectBodyState? I would like to apply limit to the ball speed something like linear_velocity but don't know the code.

You get the error because I forgot to put a var in front of sqrVelocity. Anyway I posted the code only to give you one idea how to approach the problem.

_integrate_forces() might be a good place to limit the velocity as the docs says that would also be the place to set the position if needed.

So it is probably something like this:

export var max_speed = 100.0

    if state.linear_velocity.length()>max_speed:
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.