+1 vote

this my code , it can Can simulate physical fall , but not collison with StaticBody2D

extends Node2D

var vs = VisualServer
var ps = Physics2DServer
func _ready():
    create_virus()

func create_virus():
    #创建body
    var body = ps.body_create()

    #设置模式
    ps.body_set_mode(body, ps.BODY_MODE_RIGID)
    ps.body_set_collision_layer(body,1)
    ps.body_set_collision_mask(body,1)
    #添加形状
    var shape = RectangleShape2D.new()
    shape.extents = Vector2(100, 100)
    ps.body_add_shape(body, shape)
    #设置body到当前空间
    ps.body_set_space(body, get_world_2d().space)
    #初始化位置
    var xform = Transform2D().translated(Vector2(1080/2, 1920/2))
    ps.body_set_state(body, ps.BODY_STATE_TRANSFORM, xform)
    #canvasItem
    var sprite = create_item("res://icon.png")
    ps.body_set_force_integration_callback(body, self, "_body_moved", sprite)

 func create_item(path):
    var ci_rid = VisualServer.canvas_item_create()
    VisualServer.canvas_item_set_parent(ci_rid, get_canvas_item())
    var sprite = load(path)
    VisualServer.canvas_item_add_texture_rect(ci_rid, Rect2(sprite.get_size() / 2, sprite.get_size()), sprite)
    return ci_rid

func _body_moved(state, index):
    VisualServer.canvas_item_set_transform(index, state.transform)
in Engine by (24 points)

I'm in the same situation.

Did you find the cause of the problem?

Please log in or register to answer this question.

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 webmaster@godotengine.org with your username.