Physics server area help

Godot Version

4.4

Question

I am trying to use the physics server to create monitorable areas (as bullets for a bullet hell), but cannot seem to detect them with my normal character. An area 2d node in the scene with the same details gets picked up just fine, but the physics server version seems invisible. Any help would be appreciated!

public partial class ProjectileSpawnerTest : Node2D
{
	[Export] private Texture2D texture;
	
	private Rid ci_rid;
	private Rid tex_rid;
	private Rid world;
	private Rid area;
	private Rid shape;
	
	public override void _Ready()
	{
		// Setup
		world = GetWorld2D().Space;
		
		// Canvas item
		ci_rid = RenderingServer.CanvasItemCreate();
		tex_rid = texture.GetRid();
		RenderingServer.CanvasItemSetParent(ci_rid, GetCanvasItem());
		RenderingServer.CanvasItemAddTextureRect(ci_rid, new Rect2(-texture.GetSize() / 2, texture.GetSize()), tex_rid);
		
		// Area 2D
		area = PhysicsServer2D.AreaCreate();
		shape = PhysicsServer2D.CircleShapeCreate();
		
		PhysicsServer2D.ShapeSetData(shape, 20f);
		PhysicsServer2D.AreaAddShape(area, shape);
		PhysicsServer2D.AreaSetSpace(area, world);
		PhysicsServer2D.AreaSetCollisionLayer(area, 32);
		PhysicsServer2D.AreaSetMonitorable(area, true);
		PhysicsServer2D.AreaSetShapeDisabled(area, 0 , false);
		
		
		// Transform
		Transform2D transform_canvas = new Transform2D(0, new Vector2(0.32f, 0.32f), 0, Vector2.Zero);
		Transform2D transform_projectile = Transform2D.Identity;
		PhysicsServer2D.AreaSetTransform(area, transform_projectile);
		RenderingServer.CanvasItemSetTransform(ci_rid, transform_canvas);
	}
}

PhysicsServer2D.area_set_transform() needs the global transform of the body but it’s being set to identity which means the area is positioned at 0,0 (Transform2D transform_projectile = Transform2D.Identity;)

You can use this demo godot-demo-projects/2d/bullet_shower at master · godotengine/godot-demo-projects · GitHub as a reference to what you need to do.

2 Likes

So, I actually had tried many different transforms, and confirmed they had all lined up correctly prom print commands, but you are correct that the way that I sent the code as above could have led to the canvas item and the area being mismatched in their position. Since I was testing everything at the origin it did not make a difference in this case though.

The actual problem however was the way I was detecting the area.

I was simply using the AreaEntered trigger, not even stopping to think that, hey that method returns an Area2D NODE…

Oops lol

So I tried the other version I found using intellisense, AreaShapeEntered and sure enough that did the trick :sweat_smile:

Thanks for the assist though, transform positions on the server can definitely be tricky, seeing as the canvas items must be parented to something (thus inheriting their position), while areas / bodies hang out on their own. Could easily lead to confusion where the positions are mismatched!