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):
	update()

func _ready():
	var grivaty = Physics2DServer.area_get_param(get_world_2d().get_space(),Physics2DServer.AREA_PARAM_GRAVITY_VECTOR)
	Physics2DServer.area_set_param(get_world_2d().get_space(),Physics2DServer.AREA_PARAM_GRAVITY_VECTOR,Vector2(0,98))

	bullet = Bullet.new()
	bullet.body = Physics2DServer.body_create()
	Physics2DServer.body_set_mode(bullet.body,Physics2DServer.BODY_MODE_RIGID)
	
	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)
	
	Physics2DServer.body_apply_central_impulse(bullet,Vector2(0,-600000))
  • 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