Skip to content

Queries

CP::Space#

#point_query(point : Vect, max_distance : Number = 0, filter : ShapeFilter = ShapeFilter::ALL) : Array(PointQueryInfo)#

Query the space at a point for shapes within the given distance range.

The filter is applied to the query and follows the same rules as the collision detection. Sensor shapes are included. If a max_distance of 0 is used, the point must lie inside a shape. Negative max_distance is also allowed meaning that the point must be a under a certain depth within a shape to be considered a match.

View source

#segment_query(start : Vect, end end_ : Vect, radius : Number = 0, filter : ShapeFilter = ShapeFilter::ALL) : Array(SegmentQueryInfo)#

Perform a directed line segment query (like a raycast) against the space and yield each shape intersected.

The filter is applied to the query and follows the same rules as the collision detection. Sensor shapes are included.

View source

#shape_query(shape : Shape) : Array(Shape)#

Query a space for any shapes overlapping the given shape and yield each shape found.

View source

CP::Shape#

#point_query(p : Vect) : PointQueryInfo#

Perform a nearest point query. It finds the closest point on the surface of shape to a specific point.

View source

#segment_query(a : Vect, b : Vect, radius : Number = 0) : SegmentQueryInfo | Nil#

Perform a segment query against a shape: check if the line segment from start to end intersects the shape.

View source

struct CP::PointQueryInfo
inherits Struct #

Holds the result of a point query made on a Shape or Space.

Constructors#

.new(shape : Shape, point : Vect, distance : Float64, gradient : Vect)#

View source

Methods#

#distance : Float64#

The distance to the point (negative if the point is inside the shape).

View source

#distance=(distance : Float64)#

View source

#gradient : Vect#

The gradient of the signed distance function.

The value should be similar to point/distance, but accurate even for very small values of distance.

View source

#gradient=(gradient : Vect)#

View source

#point : Vect#

The closest point on the shape's surface (in world space coordinates).

View source

#point=(point : Vect)#

View source

#shape : Shape#

The nearest shape

View source

#shape=(shape : Shape)#

View source

struct CP::SegmentQueryInfo
inherits Struct #

Segment queries return more information than just a simple yes or no, they also return where a shape was hit and its surface normal at the hit point. This object holds that information.

Segment queries are like ray casting, but because not all spatial indexes allow processing infinitely long ray queries it is limited to segments. In practice this is still very fast and you don't need to worry too much about the performance as long as you aren't using extremely long segments for your queries.

Constructors#

.new(shape : Shape, point : Vect, normal : Vect, alpha : Float64)#

View source

Methods#

#alpha : Float64#

The normalized distance along the query segment in the range [0, 1].

View source

#alpha=(alpha : Float64)#

View source

#normal : Vect#

The normal of the surface hit.

View source

#normal=(normal : Vect)#

View source

#point : Vect#

The point of impact.

View source

#point=(point : Vect)#

View source

#shape : Shape#

The shape that was hit.

View source

#shape=(shape : Shape)#

View source