RigidBody2D created with Physics2DServer is not working. (Godot 3.1)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By myhalibobo

I created one and I put it in the middle of the screen, but it doesn’t work

extends Node2D

var shape
var bullet

class Bullet:
	var pos = Vector2()
	var body = RID()

func _draw():
	var t = preload("res://bullet.png")
	var tofs = -t.get_size()*0.5
	draw_texture(t, bullet.pos + tofs)

func _process(delta):

func _ready():
	var grivaty = Physics2DServer.area_get_param(get_world_2d().get_space(),Physics2DServer.AREA_PARAM_GRAVITY_VECTOR)

	bullet = Bullet.new()
	bullet.body = Physics2DServer.body_create()
	shape = Physics2DServer.circle_shape_create ()
	Physics2DServer.shape_set_data(shape, 8)
	Physics2DServer.body_set_space(bullet.body, get_world_2d().get_space())
	Physics2DServer.body_add_shape(bullet.body, shape)
	bullet.pos = get_viewport_rect().size/2
	var mat = Transform2D()
	mat.origin = bullet.pos
	Physics2DServer.body_set_state(bullet.body, Physics2DServer.BODY_STATE_LINEAR_VELOCITY, mat)
  • Edited for clarity. ~Avencherus

Also, wouldn’t it be a lot easier if you just made a bullet scene then instance and spawn bullets wherever you need them?

SIsilicon | 2018-12-17 02:26

This is just a test that my goal is to create a large number of rigibody2d like objects

myhalibobo | 2018-12-17 09:05