0 votes

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
in Engine by (24 points)
edited by

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

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

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