#What function allows for accurate detection of CollisionShape2D overlap/collision/etc?
1 messages · Page 1 of 1 (latest)
Physics2DDirectSpaceState.intersect_shape()
Or move_and_collide (for kinematicbody2d only)
Don't those only exist in 3.x?
Normaly the First one is in godot 4
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.
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!")
Thanks, I was having a really hard time deciphering the documentation for this.
Doesn't seem to detect any collisions at all.
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.
From what I've seen the physics engine doesn't really support pixel perfect collision. I remember one person somehow made it work a while back, but I can't remember the details, and since then I've only seen people failing at it.
I think there are 2 options: make the test shape slightly smaller, or write your own collision detection without relying on the built-in physics
intersect_shape ignores the motion property from the query
When you surround some words with single backticks like `this`, it will be formatted as code.
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")
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/
Even without the motion property shouldn't it stop once it overlaps? Though it sounds like it's going to be inaccurate just like everything else.
Having a 2D game creation program that doesn't support accurate 2D collision checking seems like a pretty egregious flaw. How have they not added that in by now? Like that's the absolute most basic function necessary to make most types of 2D games. Is there really no function that checks it accurately?
If your "hole" is the exact size of the shape, meaning if the size of the shape was only 0.00000000001 bigger it would suddenly overlap that's to narrow a margin for the physics engine. From what I understand
These ones are hard to debug. You definitely wanna make sure it doesn't run too early, so the physics world space has already been updated and check for any errors/warnings in the debugger