48 if ((
self = [super
init])) {
63 if ((
self = [
self init])) {
71 if ((
self = [
self init])) {
90 return [
NSString stringWithFormat:@"<%@ %p>{ID: %d, %zu subregions, %u ents}", [
self class],
self, crid, [subregions count], n_entities];
95 [subregions makeObjectsPerformSelector:@selector(clearSubregions)];
96 [subregions removeAllObjects];
99- (void)addSubregionAtPosition:(
HPVector)pos withRadius:(GLfloat)rad
117 sub = [[
CollisionRegion alloc] initAtLocation:pos withRadius:rad withinRegion:
self];
120 [subregions addObject:sub];
134 GLfloat r1 = region->
radius;
136 if ((position.
x < loc.
x - r1) || (position.
x > loc.
x + r1) || (position.
y < loc.
y - r1) || (position.
y > loc.
y + r1) || (position.
z < loc.
z - r1) || (position.
z > loc.
z + r1)) {
151 GLfloat r1 = region->
radius;
153 if ((position.
x - rad < loc.
x - r1) || (position.
x + rad > loc.
x + r1) || (position.
y - rad < loc.
y - r1) || (position.
y + rad > loc.
y + r1) || (position.
z - rad < loc.
z - r1) || (position.
z + rad > loc.
z + r1)) {
170 if ((position.
x < loc.
x - r1) || (position.
x > loc.
x + r1) || (position.
y < loc.
y - r1) || (position.
y > loc.
y + r1) || (position.
z < loc.
z - r1) || (position.
z > loc.
z + r1)) {
181 [subregions makeObjectsPerformSelector:@selector(clearEntityList)];
193 if (new_store == NULL) {
194 [
NSException raise:NSMallocException format:@"Not enough memory to grow collision region member list."];
229 [subregions makeObjectsPerformSelector:@selector(findCollisions)];
240 double dist2, r1, r2, r0, min_dist2;
242 Entity* entities_to_test[n_entities];
245 unsigned n_entities_to_test = 0;
253#ifdef OO_DEBUG_MONITOR_ENABLED
255 OOLog(
@"collisionRegion.debug",
@"DEBUG in collision region %@ testing %d out of %d entities",
self, n_entities_to_test,
n_entities);
259 if (n_entities_to_test < 2)
264 for (i = 0; i < n_entities_to_test; i++) {
265 e1 = entities_to_test[
i];
281 for (i = 0; i < n_entities_to_test; i++) {
282 e1 = entities_to_test[
i];
300#ifdef OO_DEBUG_MONITOR_ENABLED
302 OOLog(
@"collisionRegion.debug",
@"DEBUG Testing collision between %@ (%@) and %@ (%@)",
314 if (dist2 >= min_dist2) {
324 if (dist2 < min_dist2) {
329 if ([se1 shipIsInDockingCorridor:(
ShipEntity*)e2]) {
336 if ([se2 shipIsInDockingCorridor:(
ShipEntity*)e1]) {
369#ifdef OO_DEBUG_MONITOR_ENABLED
412 if (d2_e2sun > d2_sun) {
415 if (d2_e2 < cr_e2 * cr_e2 && [e2 isShip]) {
418 GLfloat bbx = 0.0f, bby = 0.0f, bbz = 0.0f;
445 if (d2_e2 < minbb * minbb) {
455 GLfloat cr2_sun_scaled = cr_sun * cr_sun * d2_e2 / d2_sun;
456 if (cr_e2 * cr_e2 < cr2_sun_scaled) {
465 float d2_e = sqrt(d2_e2);
470 theta_diff = asin(1) - asin(cr_sun / sqrt(d2_sun));
472 theta_diff = asin(cr_e2 / d2_e) - asin(cr_sun / sqrt(d2_sun));
485 *outValue = (phi / theta_diff);
487 if (phi > theta_diff) {
518 if (the_sun ==
nil) {
522 unsigned ent_count =
UNIVERSE->n_entities;
525 unsigned n_planets = 0;
527 unsigned n_ships = 0;
529 for (i = 0; i < ent_count; i++) {
530 if (uni_entities[i]->isSunlit) {
532 if ([uni_entities[i] isPlanet]) {
539 else if ([uni_entities[i] isShip] &&
547 [subregions makeObjectsPerformSelector:@selector(findShadowedEntities)];
552 if (![e1 isVisible]) {
555 BOOL occluder_moved = NO;
556 if ([e1 status] == STATUS_COCKPIT_DISPLAY) {
563 occluder = [UNIVERSE entityForUniversalID:e1->shadingEntityID];
564 if (occluder !=
nil) {
565 occluder_moved = occluder->
hasMoved;
568 if (([e1 isShip] || [e1 isPlanet]) && (e1->
hasMoved || occluder_moved)) {
573 if ([e1 isPlayer] && ([(
PlayerEntity*)e1 showDemoShips])) {
590 for (j = 0; j < n_planets; j++) {
591 float occlusionNumber;
607 for (j = 0; j < n_ships; j++) {
620 return [
NSString stringWithFormat:@"p%u - c%u", checks_this_tick, checks_within_range];
625 NSMutableString* result = [[
NSMutableString alloc] initWithFormat:@"%d:", n_entities];
630 return [
result autorelease];
#define MINIMUM_SHADOWING_ENTITY_RADIUS
#define COLLISION_MAX_ENTITIES
#define COLLISION_REGION_BORDER_RADIUS
BOOL shadowAtPointOcclusionToValue(HPVector e1pos, GLfloat e1rad, Entity *e2, OOSunEntity *the_sun, float *outValue)
static BOOL positionIsWithinBorders(HPVector position, CollisionRegion *region)
static BOOL positionIsWithinRegion(HPVector position, CollisionRegion *region)
static BOOL sphereIsWithinRegion(HPVector position, GLfloat rad, CollisionRegion *region)
OOINLINE void bounding_box_get_dimensions(BoundingBox bb, GLfloat *xSize, GLfloat *ySize, GLfloat *zSize) ALWAYS_INLINE_FUNC
OOINLINE HPVector HPvector_subtract(HPVector a, HPVector b) INLINE_CONST_FUNC
OOINLINE Vector HPVectorToVector(HPVector v) INLINE_CONST_FUNC
OOINLINE OOHPScalar HPdistance2(HPVector v1, HPVector v2) INLINE_CONST_FUNC
#define OOLog(class, format,...)
OOINLINE Vector vector_normal_or_zbasis(Vector vec) INLINE_CONST_FUNC
OOINLINE OOScalar dot_product(Vector first, Vector second) INLINE_CONST_FUNC
OOINLINE Vector vector_normal_or_xbasis(Vector vec) INLINE_CONST_FUNC
#define PROXIMITY_WARN_DISTANCE2
NSString * collisionDescription()
void addEntity:(Entity *ent)
static BOOL positionIsWithinBorders(HPVector position, CollisionRegion *region)
static BOOL positionIsWithinRegion(HPVector position, CollisionRegion *region)
void addSubregionAtPosition:withRadius:(HPVector pos,[withRadius] GLfloat rad)
static BOOL entityByEntityOcclusionToValue(Entity *e1, Entity *e2, OOSunEntity *the_sun, float *outValue)
NSMutableArray * subregions
CollisionRegion * parentRegion
unsigned checks_this_tick
void findShadowedEntities()
static BOOL sphereIsWithinRegion(HPVector position, GLfloat rad, CollisionRegion *region)
unsigned checks_within_range
static BOOL testEntityOccludedByEntity(Entity *e1, Entity *e2, OOSunEntity *the_sun)
BOOL shadowAtPointOcclusionToValue(HPVector e1pos, GLfloat e1rad, Entity *e2, OOSunEntity *the_sun, float *outValue)
OOUniversalID universalID
NSMutableArray * collisionArray()
unsigned collisionTestFilter
OOUniversalID shadingEntityID
BOOL checkCloseCollisionWith:(Entity *other)
void setCollisionRegion:(CollisionRegion *region)
void setOcclusionLevel:(float level)
BOOL checkCloseCollisionWith:(Entity *other)
BoundingBox totalBoundingBox
void setProximityAlert:(ShipEntity *targetEntity)
BOOL shipIsInDockingCorridor:(ShipEntity *ship)