I'm trying to shapecast into the distance forward from the camera center:
var sphere: = SphereShape.new()
sphere.radius = 1.0
var vp_size = get_viewport().size
var from = cam.project_ray_origin(vp_size * 0.5)
var to = cam.project_ray_normal(vp_size * 0.5)
var space_state : PhysicsDirectSpaceState = get_world().direct_space_state as PhysicsDirectSpaceState
var param := PhysicsShapeQueryParameters.new()
param.collision_mask = player.collision_mask
param.set_shape( sphere )
param.transform = Transform.IDENTITY
param.transform.origin = from
param.margin = sphere.margin
param.exclude = [ self, player ]
var motion = to * distance
var trace := space_state.cast_motion( param, motion )
print(trace)
var collision := space_state.get_rest_info(param)
print(collision)
In this example, print(trace)
works and seems to give me the correct result depending on there being a collider directly ahead or not. However, print(collision)
only ever returns {}
, which makes no sense if trace
is working correctly. This seems to help:
var trace := space_state.cast_motion( param, motion )
param.transform.origin += motion * trace[1]
print(trace)
var collision := space_state.get_rest_info(param)
print(collision)
But it gives unreliable results, and I'd really like to know why space_state.get_rest_info(param)
is empty when space_state.cast_motion( param, motion )
is working fine (PhysicsDirectSpaceState queries are a constant learning process for me).