Munk2D Documentation  8.x.x
Loading...
Searching...
No Matches
cpBB.h
1/* Copyright (c) 2025 Victor Blomqvist
2 * Copyright (c) 2007-2024 Scott Lembcke and Howling Moon Software
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21*/
22
23#ifndef CHIPMUNK_BB_H
24#define CHIPMUNK_BB_H
25
26#include "chipmunk_types.h"
27#include "cpVect.h"
28
32
34typedef struct cpBB{
35 cpFloat l, b, r ,t;
37
39static inline cpBB cpBBNew(const cpFloat l, const cpFloat b, const cpFloat r, const cpFloat t)
40{
41 cpBB bb = {l, b, r, t};
42 return bb;
43}
44
46static inline cpBB
47cpBBNewForExtents(const cpVect c, const cpFloat hw, const cpFloat hh)
48{
49 return cpBBNew(c.x - hw, c.y - hh, c.x + hw, c.y + hh);
50}
51
53static inline cpBB cpBBNewForCircle(const cpVect p, const cpFloat r)
54{
55 return cpBBNewForExtents(p, r, r);
56}
57
59static inline cpBool cpBBIntersects(const cpBB a, const cpBB b)
60{
61 return (a.l <= b.r && b.l <= a.r && a.b <= b.t && b.b <= a.t);
62}
63
65static inline cpBool cpBBContainsBB(const cpBB bb, const cpBB other)
66{
67 return (bb.l <= other.l && bb.r >= other.r && bb.b <= other.b && bb.t >= other.t);
68}
69
71static inline cpBool cpBBContainsVect(const cpBB bb, const cpVect v)
72{
73 return (bb.l <= v.x && bb.r >= v.x && bb.b <= v.y && bb.t >= v.y);
74}
75
77static inline cpBB cpBBMerge(const cpBB a, const cpBB b){
78 return cpBBNew(
79 cpfmin(a.l, b.l),
80 cpfmin(a.b, b.b),
81 cpfmax(a.r, b.r),
82 cpfmax(a.t, b.t)
83 );
84}
85
87static inline cpBB cpBBExpand(const cpBB bb, const cpVect v){
88 return cpBBNew(
89 cpfmin(bb.l, v.x),
90 cpfmin(bb.b, v.y),
91 cpfmax(bb.r, v.x),
92 cpfmax(bb.t, v.y)
93 );
94}
95
97static inline cpVect
99{
100 return cpvlerp(cpv(bb.l, bb.b), cpv(bb.r, bb.t), 0.5f);
101}
102
104static inline cpFloat cpBBArea(cpBB bb)
105{
106 return (bb.r - bb.l)*(bb.t - bb.b);
107}
108
110static inline cpFloat cpBBMergedArea(cpBB a, cpBB b)
111{
112 return (cpfmax(a.r, b.r) - cpfmin(a.l, b.l))*(cpfmax(a.t, b.t) - cpfmin(a.b, b.b));
113}
114
117{
118 cpVect delta = cpvsub(b, a);
119 cpFloat tmin = -INFINITY, tmax = INFINITY;
120
121 if(delta.x == 0.0f){
122 if(a.x < bb.l || bb.r < a.x) return INFINITY;
123 } else {
124 cpFloat t1 = (bb.l - a.x)/delta.x;
125 cpFloat t2 = (bb.r - a.x)/delta.x;
126 tmin = cpfmax(tmin, cpfmin(t1, t2));
127 tmax = cpfmin(tmax, cpfmax(t1, t2));
128 }
129
130 if(delta.y == 0.0f){
131 if(a.y < bb.b || bb.t < a.y) return INFINITY;
132 } else {
133 cpFloat t1 = (bb.b - a.y)/delta.y;
134 cpFloat t2 = (bb.t - a.y)/delta.y;
135 tmin = cpfmax(tmin, cpfmin(t1, t2));
136 tmax = cpfmin(tmax, cpfmax(t1, t2));
137 }
138
139 if(tmin <= tmax && 0.0f <= tmax && tmin <= 1.0f){
140 return cpfmax(tmin, 0.0f);
141 } else {
142 return INFINITY;
143 }
144}
145
148{
149 return (cpBBSegmentQuery(bb, a, b) != INFINITY);
150}
151
153static inline cpVect
154cpBBClampVect(const cpBB bb, const cpVect v)
155{
156 return cpv(cpfclamp(v.x, bb.l, bb.r), cpfclamp(v.y, bb.b, bb.t));
157}
158
160static inline cpVect
161cpBBWrapVect(const cpBB bb, const cpVect v)
162{
163 cpFloat dx = cpfabs(bb.r - bb.l);
164 cpFloat modx = cpfmod(v.x - bb.l, dx);
165 cpFloat x = (modx > 0.0f) ? modx : modx + dx;
166
167 cpFloat dy = cpfabs(bb.t - bb.b);
168 cpFloat mody = cpfmod(v.y - bb.b, dy);
169 cpFloat y = (mody > 0.0f) ? mody : mody + dy;
170
171 return cpv(x + bb.l, y + bb.b);
172}
173
175static inline cpBB
176cpBBOffset(const cpBB bb, const cpVect v)
177{
178 return cpBBNew(
179 bb.l + v.x,
180 bb.b + v.y,
181 bb.r + v.x,
182 bb.t + v.y
183 );
184}
185
187
188#endif
static cpFloat cpfclamp(cpFloat f, cpFloat min, cpFloat max)
Clamp f to be between min and max.
Definition chipmunk_types.h:138
unsigned char cpBool
Chipmunk's boolean type.
Definition chipmunk_types.h:179
static cpFloat cpfabs(cpFloat f)
Return the absolute value of a cpFloat.
Definition chipmunk_types.h:132
double cpFloat
Chipmunk's floating point type.
Definition chipmunk_types.h:68
static cpFloat cpfmin(cpFloat a, cpFloat b)
Return the min of two cpFloats.
Definition chipmunk_types.h:126
static cpFloat cpfmax(cpFloat a, cpFloat b)
Return the max of two cpFloats.
Definition chipmunk_types.h:120
static cpVect cpBBWrapVect(const cpBB bb, const cpVect v)
Wrap a vector to a bounding box.
Definition cpBB.h:161
static cpBB cpBBNewForExtents(const cpVect c, const cpFloat hw, const cpFloat hh)
Constructs a cpBB centered on a point with the given extents (half sizes).
Definition cpBB.h:47
static cpBB cpBBNew(const cpFloat l, const cpFloat b, const cpFloat r, const cpFloat t)
Convenience constructor for cpBB structs.
Definition cpBB.h:39
static cpVect cpBBCenter(cpBB bb)
Returns the center of a bounding box.
Definition cpBB.h:98
static cpBB cpBBOffset(const cpBB bb, const cpVect v)
Returns a bounding box offseted by v.
Definition cpBB.h:176
static cpBool cpBBIntersectsSegment(cpBB bb, cpVect a, cpVect b)
Return true if the bounding box intersects the line segment with ends a and b.
Definition cpBB.h:147
static cpFloat cpBBMergedArea(cpBB a, cpBB b)
Merges a and b and returns the area of the merged bounding box.
Definition cpBB.h:110
static cpVect cpBBClampVect(const cpBB bb, const cpVect v)
Clamp a vector to a bounding box.
Definition cpBB.h:154
static cpBool cpBBIntersects(const cpBB a, const cpBB b)
Returns true if a and b intersect.
Definition cpBB.h:59
static cpBB cpBBMerge(const cpBB a, const cpBB b)
Returns a bounding box that holds both bounding boxes.
Definition cpBB.h:77
static cpBB cpBBExpand(const cpBB bb, const cpVect v)
Returns a bounding box that holds both bb and v.
Definition cpBB.h:87
static cpFloat cpBBArea(cpBB bb)
Returns the area of the bounding box.
Definition cpBB.h:104
static cpBool cpBBContainsBB(const cpBB bb, const cpBB other)
Returns true if other lies completely within bb.
Definition cpBB.h:65
static cpBool cpBBContainsVect(const cpBB bb, const cpVect v)
Returns true if bb contains v.
Definition cpBB.h:71
static cpFloat cpBBSegmentQuery(cpBB bb, cpVect a, cpVect b)
Returns the fraction along the segment query the cpBB is hit. Returns INFINITY if it doesn't hit.
Definition cpBB.h:116
static cpBB cpBBNewForCircle(const cpVect p, const cpFloat r)
Constructs a cpBB for a circle with the given position and radius.
Definition cpBB.h:53
static cpVect cpv(const cpFloat x, const cpFloat y)
Convenience constructor for cpVect structs.
Definition cpVect.h:36
static cpVect cpvlerp(const cpVect v1, const cpVect v2, const cpFloat t)
Linearly interpolate between v1 and v2.
Definition cpVect.h:141
static cpVect cpvsub(const cpVect v1, const cpVect v2)
Subtract two vectors.
Definition cpVect.h:55
Chipmunk's axis-aligned 2D bounding box type. (left, bottom, right, top)
Definition cpBB.h:34
Definition chipmunk_types.h:251