+1 vote

Is there a method or a better way to get specific points in a collisionBox2D?
I am programming a physics engine where the character is instantiated at runtime, and I was trying to get him to fire rays to handle the collision with slopes.

My solution works but it is far from elegant and optimized
I created a function that returns a dictionary filled with the corners of the collisionBox2D relative to the parent node(the global position)

func get_corners():
    raycast_origins = {
        top_left = self.get_global_pos() + self.get_node("hitbox").get_shape().get_extents()*Vector2(-1,-1)+Vector2(skin,skin),
        top_right = self.get_global_pos() + self.get_node("hitbox").get_shape().get_extents()*Vector2(1,-1)+Vector2(-skin,skin),
        bottom_left = self.get_global_pos() + self.get_node("hitbox").get_shape().get_extents()*Vector2(-1,1)+Vector2(skin,-skin),
        bottom_right = self.get_global_pos() + self.get_node("hitbox").get_shape().get_extents()*Vector2(1,1)+Vector2(-skin,-skin)
    return raycast_origins

#Then, a second function shots the rays:

func ray_check():
    var ray_check = {
        top_right = space_state.intersect_ray(get_corners().top_right,get_corners().top_right+Vector2(raycast_step*1,0) ,[self] ),
        middle_right = space_state.intersect_ray(get_corners().top_right-Vector2(0,self.get_node("hitbox").get_shape().get_extents().y,get_corners().top_right-Vector2(0,get_corners().top_right-Vector2(0,self.get_node("hitbox").get_shape().get_extents().y)+Vector2(raycast_step*1,0) ,[self] )))
    return ray_check

So, I'd like to know how you guys would approach this or if I am on the right path, how can I optimize the formula.

in Engine by (234 points)
edited by

1 Answer

0 votes
Best answer

Use more intermediate variables. Saying self.get_node("hitbox").get_shape().get_extents() several times is unnecessarily hard to read, and quite possibly slower (I don't know how much optimization GDScript does). In fact, it's bad enough that I'm surprised your code runs without an error: the nesting for your middle_right ray check is all wrong. Consider doing it more like this:

func ray_check():
    var p = get_global_pos()
    var extents = get_node("hitbox").get_shape().get_extents - Vector2(skin, skin)
    var top_right = p + extents*Vector2(1,-1)
    var bottom_right = p + extents*Vector2(1,1)
    var middle_right = (top_right + bottom_right) / 2
    var ray_size = Vector2(raycast_step, 0)
    return {
        top_right = space_state.intersect_ray(top_right, top_right+ray_size,[self])
        middle_right = space_state.intersect_ray(middle_right, middle_right+ray_size,[self])

Also notice that you don't need self when you're calling methods on the current object...

by (48 points)
selected by
I have to say, my brain was in loops, you know when you do something and 3 minutes after you are completely lost in how you managed to do that?
Thanks for the clarifications.
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.