Topdown game collision

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

I’m trying to make a 2d top-down Factorio-like game.
I’m having some problems with the player colliding with other objects.
The player uses kinematic-body while rocks use static-bodies. I can’t get the player to collide with the rock.

extends Node2D

var Position
const SpeedVert = 3
const SpeedHor = 3

func _ready():
	set_process(true)

func _process(delta):
	Position = get_pos()
	if Input.is_action_pressed("ui_up"):
		Position.y -= SpeedVert
	if Input.is_action_pressed("ui_down"):
		Position.y += SpeedVert
	if Input.is_action_pressed("ui_left"):
		Position.x -= SpeedHor
	if Input.is_action_pressed("ui_right"):
		Position.x += SpeedHor
	if (not get_node("PlayerBody").test_move(Position)):
		# get_node("PlayerBody").move(Position)
		set_pos(Position)

Please help

:bust_in_silhouette: Reply From: gtkampos

Are you using Godot 3.0? Maybe you want to use move_and_collide()?

See the docs: http://docs.godotengine.org/en/latest/classes/class_kinematicbody2d.html

:bust_in_silhouette: Reply From: hilfazer

Use move_to( Position ), not set_pos(), no need to call test_move().

In Godot 3 you want to call move_and_collide() but it needs vector relative to player’s position

var relativeVector = Vector2(0, 0)

put it in _process() and modify according to user input. You won’t need Position variable.

BTW do you have CollisionShape2D nodes in your body nodes?