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
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))