How to Cast a ShapeCast3D In GDScript?
Asked Answered
S

3

0

Hi,
I am trying to cast a ShapeCast3D using code
Doing this with a ray cast is simple enough:

func _physics_process(delta):
	var query = PhysicsRayQueryParameters3D.create(position, position + Vector3(0, -10, 0))
	var collision = get_world_3d().direct_space_state.intersect_ray(query)

But for the life of me, I cannot figure out how to do something similar with ShapeCast3D and I can't find any examples online to go off of. Here is code I have attempted to use (I have no idea what I'm doing 🙂

func _physics_process(delta):
	var space_state = PhysicsDirectSpaceState3D
	var thing = PhysicsShapeQueryParameters3D.new()
	thing.set_shape(PLAYER_SHAPE)
	thing.motion = Vector3(0,1,0)
	space_state.intersect_point(thing)

ERROR: (Cannot call non-static function "intersect_point()" on the class "PhysicsDirectSpaceState3D" directly. Make an instance instead.)
How could I create a ShapeCast3D using GDScript that returns with the intersection point?

Stavros answered 21/4, 2023 at 16:12 Comment(0)
S
0

Try changing:
var space_state = PhysicsDirectSpaceState3D
to:
var space_state = PhysicsDirectSpaceState3D.new()

That's what the error message is telling you.

Sales answered 21/4, 2023 at 16:24 Comment(0)
S
0

Sales
Just tried it
unfortunately still does not work 🥲

func _physics_process(delta):
	var space_state = PhysicsDirectSpaceState3D.new()
	var thing = PhysicsShapeQueryParameters3D.new()
	thing.set_shape(PLAYER_SHAPE)
	thing.motion = Vector3(0,1,0)
	space_state.intersect_point(thing)

Line 35:Native class "PhysicsDirectSpaceState3D" cannot be constructed as it is abstract.
Line 35:Static function "new()" not found in base "PhysicsDirectSpaceState3D".
i do not know what it means to be 'abstract'

Stavros answered 21/4, 2023 at 16:38 Comment(0)
S
0

An abstract class is one that cannot be instantiated directly (because it lacks a complete implementation). You can only instantiate a class that inherits it (and has a complete implementation). In this case, it looks like that would be PhysicsDirectSpaceState3DExtension.

But I have no experience with the classes you're using, and probably can't provide more help than that.

Sales answered 21/4, 2023 at 16:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.