Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | toblin |
I’m trying to optimize using servers, and I seem to having troubles getting my Areas to work. Below is what I’ve done so far. I test the class by spawning two Cells next to each other to see if the areas interact somehow. Thus far they don’t seem to interact. My feeling is that they don’t exist close to the RigidBodies.
Any ideas what might be awry?
extends Spatial
class_name Cell
var contacts = {}
var local_origo: Transform
var visualinstance
var visualmesh
var body
var body_shape
var contact_area
var contact_area_shape
func _body_moved(state, index):
# Move visualshape when physicsbody moves
VisualServer.instance_set_transform(visualinstance, state.transform)
# Move areas when physicsbody moves
PhysicsServer.area_set_transform ( contact_area, state.transform )
func _contact_body_entered(body):
contacts[body.cellname] = body
print("body entered!")
func _contact_body_exited(body):
contacts.erase(body.cellname)
print("body exited!")
func _ready():
local_origo = Transform( Basis(), Vector3(0,0,0) )
# VISUAL
# Create a visual instance (for 3D).
visualinstance = VisualServer.instance_create()
# Set the scenario from the world, this ensures it
# appears with the same objects as the scene.
VisualServer.instance_set_scenario(visualinstance, visual_scenario)
# Add a visualmesh to it.
visualmesh = SphereMesh.new()
visualmesh.radius = 1.0
VisualServer.instance_set_base(visualinstance, visualmesh)
# Move the visualmesh around.
VisualServer.instance_set_transform(visualinstance, local_origo)
# AREAs
contact_area = PhysicsServer.area_create()
contact_area_shape = SphereShape.new()
contact_area_shape.radius = 5.0
PhysicsServer.area_set_space( contact_area, physics_space )
PhysicsServer.area_add_shape( contact_area, contact_area_shape, local_origo )
PhysicsServer.area_set_transform( contact_area, local_origo )
PhysicsServer.area_set_monitor_callback(contact_area, self, "_contact_body_entered")
PhysicsServer.area_set_monitor_callback(contact_area, self, "_contact_body_exited")
# PHYSICS
# Create the body.
body = PhysicsServer.body_create()
PhysicsServer.body_set_mode(body, PhysicsServer.BODY_MODE_RIGID)
# Add a shape.
body_shape = SphereShape.new()
body_shape.radius = 1.0
# Make sure to keep the body_shape reference!
PhysicsServer.body_add_shape(body, body_shape)
# Set space, so it collides in the same space as current scene.
PhysicsServer.body_set_space(body, physics_space )
# Move initial position.
PhysicsServer.body_set_state(body, PhysicsServer.BODY_STATE_TRANSFORM, local_origo)
PhysicsServer.body_set_force_integration_callback(body, self, "_body_moved", 0)