0 votes

Hello, world! I am new in Godot and I am having trouble getting my fireball to react properly, I want them to reflect off the normals of my wedges like a mirror when I shoot. I decided to use RigidBody2D for this reason. But I cannot get the direction to change at each collision.
Please help me solve this issue, and understand the solution, thanks.

This is my script

extends RigidBody2D

var velocity = Vector2()
const speed = 60
var direction = 1
#var direction = Vector2()

func set_fireball_direction(dir):
    direction = dir
    if dir == -1:
        $AnimatedSprite.flip_h = true

func _physics_process(delta):
    velocity.x = speed * delta * direction

func _on_Area2D_body_entered(body):
    if "TileMap" in body.name:
in Engine by (12 points)

So this is not an answer, but I wondering about little things for trying to help you.
I'm not sure what your trying to achieve, but there are maybe many solutions.

I'm not an expert about rigidbody2D but I may have done some test with it.
With a Mode set to Rigid, it should collide and bounce without any code processing, I think you should use something different from area2D like a StaticBody2D, and if you dont want others part to collide or interact with it, collision layer and mask must be the solution (read the infobulle help when hovering your cursor on it).

Maybe I'm wrong but it would be more simplier to have a look with the real project if you can ?

I also have somecode suggestion for helping (don't worry, I've also done those weird things at first) :

# if "TileMap" in body.name:
if body is TileMap :

# this will set the bounciness and should be set with a physics_material_override in the editor
# it only tell how much the rigidbody2D is bouncy, and don't do anything than that

I want something like this but I don't have an idea how to implement it
Fireball reflection
So the idea is when press button for shooting a fireball. It hits to the mirror and then struggles from that mirror

1 Answer

0 votes

Ha it's a really cool mechanic indeed. Let me tell you what I think, I've never done it, but how I would process trying to do (don't take everything as the right answer I've never done it, I could be wrong on some things).

So first, seeing the demo, the fireball doesn't have any gravity physics (it doesn't fall), so it maybe more simple to do it with straight motions : it means rigidbody2d would be a nightmare to handle in this case (the physic engine somehow have randomness, so it will difficult to get a rectiligne motion).

How I would try to do it is by using a simple sprite2D (or animated sprite, whatever) with an area2d (and his collision shape) for both the fireball and the mirror.
The fireball sprite keep a vector motion, telling in which direction it is emmitted :

extends Sprite
const   SPEED   = 16
var direction   = Vector2.ZERO
func _physics_process(delta):
    position += direction * SPEED

Each mirror should emit a signal to the fireball ("area_entered"), telling in which normalized vector it should move. For example going down would be :

Vector2.DOWN = Vector2(0,1) # remember Y is reversed

Somehow each mirror should check in wich direction the fireball should enter, and in which new direction it should change. There are 4 possibilities keep those in a dictionary if needed :

extends Sprite
var check = {}
func register(enter:Vector2,leave:Vector2):
    var queue = {}
    queue.enter = enter
    queue.leave = leave
    return queue    
func _ready():
    check.A = register(Vector2.LEFT     ,Vector2.DOWN)
    check.B = register(Vector2.RIGHT    ,Vector2.UP)
    check.C = register(Vector2.UP       ,Vector2.LEFT)
    check.C = register(Vector2.DOWN     ,Vector2.RIGHT)

For the rest the difficult part is to understand how signals works, also having a class_name for the fireball and the mirror would be another possibility to avoid other things interacting but colliding layer is more simplier (because only the fireball could enter on it)

Hope this will help you in the right direction, ask me anything if needed.
Don't forget those youtube goods tutorials on godot :

by (58 points)
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 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 webmaster@godotengine.org with your username.