0 votes

Hi all....

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

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. :-)

in Engine by (828 points)

1 Answer

+1 vote
Best answer

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).

by (7,910 points)
selected by

.... 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.

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

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 Frequently asked questions and 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 [email protected] with your username.