appsinacup / godot-box2d

Godot Box2D – C++ Godot Extension that integrates the Box2D physics engine.
MIT License
329 stars 15 forks source link

Raycast2D does not work when Area2D scale x is -1 #113

Open ChocolatePinecone opened 6 months ago

ChocolatePinecone commented 6 months ago

Describe the bug A simple raycast does not wield a result when the Area2D is mirrored (meaning scale.x = -1)

To Reproduce Steps to reproduce the behavior:

  1. Create an empty Node2D scene and add a Area2D with a Rectangle shaped Collider
  2. Add an Area2D, position x: 500 y: 100
  3. Add a CollisionShape2D, size x: 200 y: 100
  4. Add the following script to the Area2D for drawing debug data:
    
    extends Area2D

var intersection_detected := false

func _process(delta): var mousepos := get_global_mouse_position()

var space := get_world_2d().space
var space_state = PhysicsServer2D.space_get_direct_state(space)
var query := PhysicsRayQueryParameters2D.create(mousepos, global_position)
query.collide_with_areas = true
query.collide_with_bodies = false
var result := space_state.intersect_ray(query)

intersection_detected = not result.is_empty()
queue_redraw()

func _draw(): if intersection_detected: $CollisionShape2D.shape.draw(get_canvas_item(), Color.YELLOW) else: $CollisionShape2D.shape.draw(get_canvas_item(), Color.WHITE)

var mousepos := to_local(get_global_mouse_position())
draw_line(mousepos, Vector2.ZERO, Color.BLUE)

4. Now run the scene. You should see the collider in yellow, indicating a succesful raycast intersection with it.
5. Stop running the scene and change the Area2D scale x to -1
6. Run the scene again. You should now see the collider in white, indicating a raycast intersection could not be found, even though the  drawn raycast line is clearly intersecting the collider.

**Expected behavior**
The raycast to return a correctly detected intersection

**Versions:**
 - OS: Windows 11 Business
 - Godot 4.2.1
 - Box2d 0.9.9