RigidBody2D not reporting is_colliding() when making contact with a kinematicBody2D

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

Hi all…

Still working on Pong. Harder than I thought, great learning experience though. :slight_smile:

I have a ball which is a RigidBody2D. It bounces around nicely.

I have some bats. They are KinematicBody2D. They are controlled nicely now with keyboard, mouse and touch. So far so good.

I also have two walls and the ball bounces nicely off the wall. The bat also stops when it hits the wall. The whole thing is just smooth as silk. The days are looking bright!

Here’s an example of each:

Ball:

  • RigidBody2D
    – sprite
    – CollisionShape2D

Bat:

  • KinematicBody2D
    – sprite
    – CollisionShape2D

Wall:

  • Node2D
    – Sprite2D
    – StaticBody2D
    – CollisionShape2D

They work nicely. I’m happy.

So now, within the bat, I’m trying to determine what we’re hitting (wall or ball) and then soon, where.

This is my code:

	#let's see if the ball hit the bat
	if bat.PlayerNumber == 1:
		if bat.is_colliding():
			print("collision event captain!")
			bat.get_collider()
			print("colided with: ", str(bat.get_collider()))

Result:

  • If bat hits wall. All strings display including the collision object
  • If bat hits ball. Nothing is displayed. No collision event captain! or anything after.

It has to be a beginner mistake. Can anyone spot what it could be?
The bat will not go through the wall. The ball bounces off the bat. It’s all working well. Just no collider info is returned.

Any advice will be lapped up. :slight_smile:

:bust_in_silhouette: Reply From: eons

The method is_colliding only works after move, if move does not hit anything, the colliding flag will stay false.

You can make the ball detect collisions instead, with contact monitor and contacts reported > 0 (or 1, I can think of a double hit against bat and wall).

… and that solved it. I had a moment where it DID report a contact just earlier. I couldn’t figure it out, but now I know. It was because I was moving the bat. Ha! Thank you for that. Now it makes sense. I’ll move the code to the ball.

Robster | 2017-02-20 12:05

For anyone in the future. I moved this code to the ball object’s code and it works perfectly:

func _ready():
	# Called every time the node is added to the scene.
	set_fixed_process(true)

func _fixed_process(delta):
	# Called every frame
	checkCollissions(delta)

#let's see if the ball hits stuff
func checkCollissions(delta):
	var collisionList = get_colliding_bodies()
	if(get_colliding_bodies().size()>0 ):
		print(collisionList[0].get_name())

Every time the ball hits something it outputs its name. For example batL, batR, wall, etc

Robster | 2017-02-21 23:13