1

My goal is detecting the RID when mouse is hovering it and the next goal is to move the RID when the mouse click on it. I have one idea to make this work which is to make another RID which follows the mouse and detect when colliding but just wondering if there's a proper way of getting RID with mouse.

func Create_collision(mouse_position):
   xform = Transform2D(1.0 , mouse_position)
   #var _shared_Area = Area2D.new()
   body = Physics2DServer.body_create()
   Physics2DServer.body_set_mode(body, Physics2DServer.BODY_MODE_RIGID)
   var _circle_shape = Physics2DServer.circle_shape_create()
   Physics2DServer.shape_set_data(_circle_shape, 128)
   Physics2DServer.area_add_shape(body, _circle_shape, xform)
   Physics2DServer.body_set_space(body, get_world_2d().space)
   Physics2DServer.body_set_state(body, Physics2DServer.BODY_STATE_TRANSFORM, Transform2D(0, Vector2(10, 20)))
   Physics2DServer.body_set_force_integration_callback(body, self, "_body_moved", 0)
   return _circle_shape.get_id()

func _body_moved(state, index):
    # Created your own canvas item, use it here.
    #VisualServer.canvas_item_set_transform(canvas_item, state.transform)   
    print("moving ojbect",state, index)
    

another way is to use intersect_point but it does work with RID ojbect for some reason: example code

func mouse_detect():
    #print("get_space()" , get_world_2d().get_space().get_id())
    
    #var space = get_world_2d()
    var space = get_world_2d().get_direct_space_state()
    
    #var space = get_canvas_item()
    # Get the mouse's position
    var mousePos = get_global_mouse_position()
    # Check if there is a collision at the mouse position
    #if space.intersect_point(mousePos, 1, [], 2147483647, true, true):
        #print("hit something",mousePos)
    
    if space.intersect_point(mousePos, 1 , [] ,2147483647 , true , true ):
        print("hit something",mousePos ,space)
        
        
    else:
        print("no hit")
        #pass
enter code here
Theraot
  • 31,890
  • 5
  • 57
  • 86
  • 1
    I would suggest `intersect_point`, and it looks correct at first glance… Are you using extra `Viewport`s? That could be messing up the coordinates. Aside from that, perhaps you need to make the the body or area monitorable (e.g. `area_set_monitorable`). Also make sure the `collision_layer` is non-zero. Refer to [FauxBody2D](https://stackoverflow.com/a/71622366/402022). – Theraot Jul 29 '22 at 02:00
  • the mouse_detect() function is finally working with the RID i added this fucntion (Physics2DServer.area_set_space(body, get_world_2d().space)) – alex hernandez Jul 29 '22 at 03:03
  • i also added the *** area_set_monitoable*** it started working with the mouse_detect i need to figure out how to get the id of the collision now – alex hernandez Jul 29 '22 at 03:05
  • If you are using `interect_point` and you want the RID, you can get it from the returned value. See the documentation on [`interect_point`](https://docs.godotengine.org/en/stable/classes/class_physics2ddirectspacestate.html#class-physics2ddirectspacestate-method-intersect-point). You an array of dictionaries, and one of the keys is `"rid"`. – Theraot Jul 29 '22 at 07:44
  • I believe the key `"collider_id` holds an instance id, see [`instance_from_id`](https://docs.godotengine.org/en/stable/classes/class_%40gdscript.html#class-gdscript-method-instance-from-id) and [`get_instance_id`](https://docs.godotengine.org/en/stable/classes/class_object.html#class-object-method-get-instance-id). – Theraot Jul 29 '22 at 07:46
  • i got it working just i (print(space.intersect_point(mousePos, 1, [], 2147483647, true, true))) and it return an arrary of object ids also this case is close but im leaving this if anyone needs help thank you so much for helping – alex hernandez Jul 29 '22 at 18:25
  • I would like to encourage you to write an answer. – Theraot Jul 29 '22 at 18:26
  • hey theraot do you have any big project in godot or any other cause seems like you have a lot of experience in programming/coding – alex hernandez Jul 29 '22 at 18:28
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/246891/discussion-between-theraot-and-alex-hernandez). – Theraot Jul 29 '22 at 18:43

1 Answers1

1

the best way to get Collision RID by mouse is using the mouse_detect() function also this is also the most optimized way. and when adding a image use the visual server with a circle image cause when the VisualServer.canvas_item_add_circle it causes performance issue by 10 times less speed.

var xform : Transform2D
func Create_collision(mouse_position):
    xform = Transform2D(1.0 , mouse_position)
    var _shared_Area = Area2D.new()
    var _circle_shape = Physics2DServer.circle_shape_create()
    Physics2DServer.shape_set_data(_circle_shape, 64)
    Physics2DServer.area_add_shape(_shared_Area.get_rid(), _circle_shape, xform)
    Physics2DServer.area_set_space(_shared_Area, get_world_2d().space)
    Physics2DServer.area_set_monitorable(_shared_Area, true)

func _process(delta:float ) -> void:
    mouse_detect()

func mouse_detect():
    var space = get_world_2d().get_direct_space_state()
    var mousePos = get_global_mouse_position()
    if space.intersect_point(mousePos, 1, [], 2147483647, true, true):
        print(space.intersect_point(mousePos, 1, [], 2147483647, true, true))