Can someone help me with my kinematic collisions?

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

I’m having problems getting collisions to register correctly. it seems only certain parts of the collision shape work when colliding. in the video i have a kinematic character hitting a rigidbody and does nothing head on. but when the side hits it, it moves. There also is a lot of jitter when the side runs into different objects. Same problem with Staticbody as well.

theres the video and here’s the code for my kinematicbody movement:

extends KinematicBody2D
var speed = 0
var backSpeed = 0
var accelSpeed = 2
var decelSpeed = 1
var rotSpeed = PI/6
var vel = Vector2()

func _fixed_process(delta):
	var rot = get_rot()
	
	if Input.is_key_pressed(KEY_RIGHT):
		set_rot(rot - rotSpeed * delta * ((speed + backSpeed)/150.0))
	
	elif Input.is_key_pressed(KEY_LEFT):
		set_rot(rot + rotSpeed * delta * ((speed + backSpeed)/150.0))
	
	if Input.is_key_pressed(KEY_UP):
		if (backSpeed < 0):
			backSpeed += decelSpeed
			vel = Vector2(sin(rot), cos(rot)) * backSpeed
		elif (speed < 150):
			speed += accelSpeed
			vel = Vector2(sin(rot), cos(rot)) * speed
		else:
			vel = Vector2(sin(rot), cos(rot)) * speed
	
	elif Input.is_key_pressed(KEY_DOWN):
		if (speed > 0):
			speed -= decelSpeed
			vel = Vector2(sin(rot), cos(rot)) * speed
		elif (backSpeed > -150):
			backSpeed -= accelSpeed
			vel = Vector2(sin(rot), cos(rot)) * backSpeed
		else:
			vel = Vector2(sin(rot), cos(rot)) * backSpeed
	else:
		if (speed > 0):
			speed -= decelSpeed
			vel = Vector2(sin(rot), cos(rot)) * speed
		elif (backSpeed < 0):
			backSpeed += decelSpeed
			vel = Vector2(sin(rot), cos(rot)) * backSpeed
		else:
			vel = Vector2(0,0)
	move_and_slide(vel)
	  	
	
	
func _ready():
	set_fixed_process(true)

I’m pretty sure I’m doing something wrong with the movement and I’ve tried to look into different ways to do it. When I tested is_colliding it onyl came back true when it was hitting the sides. I’m a self-taught novice so I’m probably missing something obvious. The version of godot I’m using is 2.1.4 stable. Thanks!

:bust_in_silhouette: Reply From: mollusca

If you want your KinematicBody2D to push RigidBodies when using the move methods you’ll have to apply an impulse to the RigidBody2D yourself. I’m not sure what causes the glitchy movement but it could be the move_and_slide method, you might have to use move instead and implement sliding yourself. Something like:

var move_left = move(vel)
if move_left.length() > 0.0:
    var collider = get_collider()
    if collider.get_type() == "RigidBody2D":
        collider.apply_impulse(Vector2(), vel)
	
    var normal = get_collision_normal()
    move_left = normal.slide(move_left)
    vel = normal.slide(vel)
    move(move_left)

If your game requires the player character to interact with RigidBody2Ds a lot it might be easier to make the player character a RigidBody2D as well.

Thanks for the reply. I tried just pasting in what you wrote and it seems to work alright. I’m still trying to wrap my head around what you’re actually doing. You have a blank Vector2 as an argument for apply_impulse, is that right? I need to mess around more with what I got before I can post more info. Maybe another video showing more of the problem. My game will mostly deal with staticbody, i just threw in a rigid body to test collision since the static bodies were acting weird. Do you think it would be better for staticbody to just do Area2D and code my ship to steer away when entering rather than figuring out what to do with a collision with a collisionshape or polygon?

thatsalottadimp | 2017-12-26 06:20

i changed my movement method and added the manual slide you had at the bottom and it seems to work with staticbodies to my liking now. I don’t know exactly what it was in the original code that made it jitter and move around so much but thank you for that. I need to mess around with the rigidbody settings more like mass and weight to get a realistic boat to boat type crash.

thatsalottadimp | 2017-12-28 06:19