Getting the movement of a KinematicBody2D

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By mapa17
:warning: Old Version Published before Godot 3 was released.

Hi,

i am writing a mini Breakout game, following this tutorial

where you have a KinematicBody2D as a paddle that collides with Ball (RigidBody2D)
The Paddel is moved based on the mouse position (using set_position() )

What i want to achieve is give the ball a little spin based on the movement of the paddle. So the reflection of the ball does not depend only on the angle it came in, but in addition on the movement of the paddle. I imaged doing this by taking the movement direction of the paddle and influencing the reflection vector of the ball based on that.

My problem is that i don’t know how to get the movement vector of the paddle. (Kinemagic Body 2D) i tried get_travel() but it always returns (0, 0).

What means are there to get the movement of the paddle?
Why is the get_travel() returning 0, 0? Because i use set_pos to move the paddle?

Thx

:bust_in_silhouette: Reply From: avencherus

You can calculate a velocity member variable based on last frame movement inside the paddle KB code. get_node("platform").velocity

extends KinematicBody2D

func _ready():
	set_fixed_process(true)

var prev_pos = get_pos()
var velocity = Vector2()

func _fixed_process(delta):
	velocity = get_pos() - prev_pos
	prev_pos = get_pos()

Thank you very much, i can solve it like this, but than I am still curious to know what

get_travel()

is supposed to be returning?

mapa17 | 2017-10-12 21:13

Your guess seems to be right: get_travel() does return (0, 0) because you use set_pos() to move the paddle. I think it is not entirely clear on the docs that only the move() function is considered (and move_and_slide and friends as well).

For avencherus, velocity is is the change of position over time. Thus, the position change should be divided by delta time: (get_pos() - prev_pos) / delta

Tegu | 2017-10-13 07:58

The first part is correct, you only can make use of the Kinematic functions if you use move(), which is the intended way of doing it, but if you’re doing something different, that was the more direct way to get you going along with your project.

@Tegu

It depends on what you’re trying to do. The assumption in my code is that you’ve already applied delta to your movement. Checking inside the loop that is happening in 1/fps the difference in movement is already time sliced. Dividing out delta tells you the velocity over one second, which may be something you want to know outside of the loop for other determinations, but if you applied that in the game loop frame by frame it would be a bug.

In here I used the move() using delta, and extracting per frame the velocity. The print out will read (1.666667, 0) (1.666672, 0) (100.000298, 0)

extends KinematicBody2D

func _ready(): set_fixed_process(true)


var motion = 100.0 # 100 pixels per second

var velocity = Vector2()
var tegu_vel = Vector2()
var prev_pos = get_pos()

func _fixed_process(delta):

	printt(get_travel(), velocity, tegu_vel)
	
	move(Vector2(motion, 0) * delta)

	velocity = get_pos() - prev_pos
	tegu_vel = (get_pos() - prev_pos) / delta


	prev_pos = get_pos()

avencherus | 2017-10-13 11:41