How to get Bounding box in collisionBox2D godot

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

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.

:bust_in_silhouette: Reply From: JoshGrams

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…

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.

brunosxs | 2016-02-26 13:26