void _add_plane(const Vector2 &p_normal, real_t p_d) { Physics2DServer *ps = Physics2DServer::get_singleton(); Array arr; arr.push_back(p_normal); arr.push_back(p_d); RID plane = ps->shape_create(Physics2DServer::SHAPE_LINE); ps->shape_set_data(plane, arr); RID plane_body = ps->body_create(Physics2DServer::BODY_MODE_STATIC); ps->body_set_space(plane_body, space); ps->body_add_shape(plane_body, plane); }
void _add_concave(const Vector<Vector2> &p_points, const Matrix32 &p_xform = Matrix32()) { Physics2DServer *ps = Physics2DServer::get_singleton(); VisualServer *vs = VisualServer::get_singleton(); RID concave = ps->shape_create(Physics2DServer::SHAPE_CONCAVE_POLYGON); ps->shape_set_data(concave, p_points); RID body = ps->body_create(Physics2DServer::BODY_MODE_STATIC); ps->body_set_space(body, space); ps->body_add_shape(body, concave); ps->body_set_state(body, Physics2DServer::BODY_STATE_TRANSFORM, p_xform); RID sprite = vs->canvas_item_create(); vs->canvas_item_set_parent(sprite, canvas); vs->canvas_item_set_transform(sprite, p_xform); for (int i = 0; i < p_points.size(); i += 2) { vs->canvas_item_add_line(sprite, p_points[i], p_points[i + 1], Color(0, 0, 0), 2); } }
void _create_body_shape_data() { VisualServer *vs = VisualServer::get_singleton(); Physics2DServer *ps = Physics2DServer::get_singleton(); // SEGMENT { DVector<uint8_t> pixels; pixels.resize(32*2*2); for(int i=0;i<2;i++) { for(int j=0;j<32;j++) { pixels.set(i*32*2+j*2+0,(j==0)?255:0); pixels.set(i*32*2+j*2+1,255); } } Image image(32,2,0,Image::FORMAT_LA8,pixels); body_shape_data[Physics2DServer::SHAPE_SEGMENT].image=vs->texture_create_from_image(image); RID segment_shape = ps->shape_create(Physics2DServer::SHAPE_SEGMENT); Rect2 sg(Point2(-16,0),Point2(16,0)); ps->shape_set_data(segment_shape,sg); body_shape_data[Physics2DServer::SHAPE_SEGMENT].shape = segment_shape; } // CIRCLE { DVector<uint8_t> pixels; pixels.resize(32*32*2); for(int i=0;i<32;i++) { for(int j=0;j<32;j++) { bool black=Vector2(i-16,j-16).length_squared() < 16*16; pixels.set(i*32*2+j*2+0,(i==16 || j==16)?255:0); pixels.set(i*32*2+j*2+1,black?255:0); } } Image image(32,32,0,Image::FORMAT_LA8,pixels); body_shape_data[Physics2DServer::SHAPE_CIRCLE].image=vs->texture_create_from_image(image); RID circle_shape = ps->shape_create(Physics2DServer::SHAPE_CIRCLE); ps->shape_set_data(circle_shape,16); body_shape_data[Physics2DServer::SHAPE_CIRCLE].shape = circle_shape; } // BOX { DVector<uint8_t> pixels; pixels.resize(32*32*2); for(int i=0;i<32;i++) { for(int j=0;j<32;j++) { bool black=i>0 && i<31 && j>0 && j<31; pixels.set(i*32*2+j*2+0,black?0:255); pixels.set(i*32*2+j*2+1,255); } } Image image(32,32,0,Image::FORMAT_LA8,pixels); body_shape_data[Physics2DServer::SHAPE_RECTANGLE].image=vs->texture_create_from_image(image); RID rectangle_shape = ps->shape_create(Physics2DServer::SHAPE_RECTANGLE); ps->shape_set_data(rectangle_shape,Vector2(16,16)); body_shape_data[Physics2DServer::SHAPE_RECTANGLE].shape = rectangle_shape; } // CAPSULE { DVector<uint8_t> pixels; pixels.resize(32*64*2); for(int i=0;i<64;i++) { for(int j=0;j<32;j++) { int si = i>48 ? i - 32 : (i<16 ? i : 16); bool black=Vector2(si-16,j-16).length_squared() < 16*16; pixels.set(i*32*2+j*2+0,(i==16 || j==16 || i==48)?255:0); pixels.set(i*32*2+j*2+1,black?255:0); } } Image image(32,64,0,Image::FORMAT_LA8,pixels); body_shape_data[Physics2DServer::SHAPE_CAPSULE].image=vs->texture_create_from_image(image); RID capsule_shape = ps->shape_create(Physics2DServer::SHAPE_CAPSULE); ps->shape_set_data(capsule_shape,Vector2(16,32)); body_shape_data[Physics2DServer::SHAPE_CAPSULE].shape = capsule_shape; } // CONVEX { Image image(convex_png); body_shape_data[Physics2DServer::SHAPE_CUSTOM+1].image=vs->texture_create_from_image(image); RID convex_polygon_shape = ps->shape_create(Physics2DServer::SHAPE_CONVEX_POLYGON); DVector<Vector2> arr; Point2 sb(32,32); arr.push_back(Point2(20,3)-sb); arr.push_back(Point2(58,23)-sb); arr.push_back(Point2(55,54)-sb); arr.push_back(Point2(27,60)-sb); arr.push_back(Point2(5,56)-sb); arr.push_back(Point2(4,20)-sb); arr.push_back(Point2(11,7)-sb); ps->shape_set_data(convex_polygon_shape,arr); body_shape_data[Physics2DServer::SHAPE_CUSTOM+1].shape = convex_polygon_shape; } }