#What function allows for accurate detection of CollisionShape2D overlap/collision/etc?

1 messages · Page 1 of 1 (latest)

delicate depot
#

Can't seem to find a function to check for CollisionShape2D overlap accurately enough for a shape to fit into an equally sized hole without erroneously detecting a collision. ShapeCast2D is equally inaccurate.

rich tulip
#

Physics2DDirectSpaceState.intersect_shape()

#

Or move_and_collide (for kinematicbody2d only)

delicate depot
#

Don't those only exist in 3.x?

willow goblet
#

characterbody*

#

don't areas work in this case?

rich tulip
#

Normaly the First one is in godot 4

delicate depot
#

Appears to be called PhysicsDirectSpaceState2D in 4.x
I'll see if it works. move_and_collide with a CharacterBody2D doesn't work, I just tried it; it has the same inaccuracy as the function does with any other Body2D.

#

Can confirm that Area2D doesn't work either. Exact same problem.

rich tulip
#

var space_state = get_world_2d().direct_space_state
var shape = CollisionShape2D.new()
shape.shape = $CollisionShape2D.shape # Use the current collision shape

var query = PhysicsShapeQueryParameters2D.new()
query.shape = shape.shape
query.transform = global_transform
query.collide_with_bodies = true
query.collide_with_areas = false

var result = space_state.intersect_shape(query)
if result.size() > 0:
print("Collision detected!")
else:
print("Clear!")

delicate depot
#

Thanks, I was having a really hard time deciphering the documentation for this.

delicate depot
#

Doesn't seem to detect any collisions at all.

delicate depot
#

shape = CollisionShape2D.new()
shape.shape = $CharacterBody2D/CollisionShape2D2.shape
query = PhysicsShapeQueryParameters2D.new()
query.shape = shape.shape
query.transform = global_transform
query.collide_with_bodies = true
query.collide_with_areas = false
query.motion = Vector2(0,1)

space_state = get_world_2d().direct_space_state
result = space_state.intersect_shape(query)
if not result.size() > 0:
velocity.y += 1

Did I do this wrong? Trying to make something move downwards until it hits something to test the function, but it doesn't seem to stop.

lost idol
lost idol
molten socketBOT
#
Embedding code in discord messages
Inline Code

When you surround some words with single backticks like `this`, it will be formatted as code.

Code Blocks

You can also include code blocks by surrounding the code with three backticks. If you add "swift" then you will also get basic syntax highlighting:
```swift
print("hello world")
```
Discord will then show it as a code block like this:

print("hello world")
Upload

If your code snippet is rather long, you can upload it to a text paste site. One option that supports syntax highlighting for GDScript is https://bpa.st/

delicate depot
delicate depot
lost idol
lost idol