c_Asteroid::c_Asteroid(sf::RenderWindow& renderer, int size, sf::Vector2i pos) : c_GameWorldObject(renderer, 0.0f, 0.5f, 1.5f, 0.0f), _size(size), _minVary(-20), _maxVary(20){ //Create a cricle which will give us the coordinates to create the shape sf::CircleShape circle(60 / size, 20 / size); _shape.setPointCount(circle.getPointCount()); for (int i = 0; i < circle.getPointCount(); i++) { _shape.setPoint(i, sf::Vector2f(circle.getPoint(i).x, circle.getPoint(i).y)); } _shape.setOutlineThickness(1.0f); _shape.setFillColor(sf::Color::Transparent); _shape.setOutlineColor(sf::Color::Yellow); //Apply the deformations std::srand((int)this); for (int i = 0; i < _shape.getPointCount(); i++) { int vary = std::rand() % _maxVary - _minVary; float x = _shape.getPoint(i).x; float y = _shape.getPoint(i).y; _shape.setPoint(i, sf::Vector2f(x + vary, y + vary)); } float xCenter = _shape.getPoint(0).x; float yCenter = _shape.getPoint(_shape.getPointCount() / 2).y / 2; //The origin is the point of the shape used to set the pos, rotate the shape, etc. _shape.setOrigin(xCenter, yCenter); _rotationalSpeed = (std::rand() % 2) + 1; //Set the direction and velocity of the 'roid float xN = (FRAND(0, 2) - 1.0f) * _size * 2; float yN = (FRAND(0, 2) - 1.0f) * _size * 2; _x = pos.x; _y = pos.y; _vX = xN; _vY = yN; }
void Warp_EPoint (VECTOR TPoint, VECTOR EPoint, TPATTERN *TPat) { VECTOR PTurbulence,RP; int Axis,i,temp_rand; int blockX = 0, blockY = 0, blockZ = 0 ; SNGL BlkNum; DBL Length; DBL Strength; WARP *Warp=TPat->Warps; TURB *Turb; TRANS *Tr; REPEAT *Repeat; BLACK_HOLE *Black_Hole; VECTOR Delta, Center; Assign_Vector(TPoint, EPoint); while (Warp != NULL) { switch(Warp->Warp_Type) { case CLASSIC_TURB_WARP: if ((TPat->Type == MARBLE_PATTERN) || (TPat->Type == NO_PATTERN) || (TPat->Type == WOOD_PATTERN)) { break; } /* If not a special type, fall through to next case */ case EXTRA_TURB_WARP: Turb=(TURB *)Warp; DTurbulence (PTurbulence, TPoint, Turb); TPoint[X] += PTurbulence[X] * Turb->Turbulence[X]; TPoint[Y] += PTurbulence[Y] * Turb->Turbulence[Y]; TPoint[Z] += PTurbulence[Z] * Turb->Turbulence[Z]; break; case NO_WARP: break; case TRANSFORM_WARP: Tr=(TRANS *)Warp; MInvTransPoint(TPoint, TPoint, &(Tr->Trans)); break; case REPEAT_WARP: Repeat=(REPEAT *)Warp; Assign_Vector(RP,TPoint); Axis=Repeat->Axis; BlkNum=(SNGL)floor(TPoint[Axis]/Repeat->Width); RP[Axis]=TPoint[Axis]-BlkNum*Repeat->Width; if (((int)BlkNum) & 1) { VEvaluateEq(RP,Repeat->Flip); if ( Repeat->Flip[Axis] < 0 ) { RP[Axis] = Repeat->Width+RP[Axis]; } } VAddScaledEq(RP,BlkNum,Repeat->Offset); Assign_Vector(TPoint,RP); break; case BLACK_HOLE_WARP: Black_Hole = (BLACK_HOLE *) Warp ; Assign_Vector (Center, Black_Hole->Center) ; if (Black_Hole->Repeat) { /* first, get the block number we're in for each dimension */ /* block numbers are (currently) calculated relative to 0 */ /* we use floor () since it correctly returns -1 for the first block below 0 in each axis */ /* one final point - we could run into overflow problems if the repeat vector was small and the scene very large. */ if (Black_Hole->Repeat_Vector [X] >= Small_Tolerance) blockX = (int) floor (TPoint [X] / Black_Hole->Repeat_Vector [X]) ; if (Black_Hole->Repeat_Vector [Y] >= Small_Tolerance) blockY = (int) floor (TPoint [Y] / Black_Hole->Repeat_Vector [Y]) ; if (Black_Hole->Repeat_Vector [Z] >= Small_Tolerance) blockZ = (int) floor (TPoint [Z] / Black_Hole->Repeat_Vector [Z]) ; if (Black_Hole->Uncertain) { /* if the position is uncertain calculate the new one first */ /* this will allow the same numbers to be returned by frand */ temp_rand = POV_GET_OLD_RAND(); /*protect seed*/ POV_SRAND (Hash3d (blockX, blockY, blockZ)) ; Center [X] += FRAND () * Black_Hole->Uncertainty_Vector [X] ; Center [Y] += FRAND () * Black_Hole->Uncertainty_Vector [Y] ; Center [Z] += FRAND () * Black_Hole->Uncertainty_Vector [Z] ; POV_SRAND (temp_rand) ; /*restore*/ } Center [X] += Black_Hole->Repeat_Vector [X] * blockX ; Center [Y] += Black_Hole->Repeat_Vector [Y] * blockY ; Center [Z] += Black_Hole->Repeat_Vector [Z] * blockZ ; } VSub (Delta, TPoint, Center) ; VLength (Length, Delta) ; /* Length is the distance from the centre of the black hole */ if (Length >= Black_Hole->Radius) break ; if (Black_Hole->Type == 0) { /* now convert the length to a proportion (0 to 1) that the point is from the edge of the black hole. a point on the perimeter of the black hole will be 0.0 ; a point at the centre will be 1.0 ; a point exactly halfway will be 0.5, and so forth. */ Length = (Black_Hole->Radius - Length) / Black_Hole->Radius ; /* Strength is the magnitude of the transformation effect. firstly, apply the Power variable to Length. this is meant to provide a means of controlling how fast the power of the Black Hole falls off from its centre. if Power is 2.0, then the effect is inverse square. increasing power will cause the Black Hole to be a lot weaker in its effect towards its perimeter. finally we multiply Strength with the Black Hole's Strength variable. if the resultant value exceeds 1.0 we clip it to 1.0. this means a point will never be transformed by more than its original distance from the centre. the result of this clipping is that you will have an 'exclusion' area near the centre of the black hole where all points whose final value exceeded or equalled 1.0 were moved by a fixed amount. this only happens if the Strength value of the Black Hole was greater than one. */ Strength = pow (Length, Black_Hole->Power) * Black_Hole->Strength ; if (Strength > 1.0) Strength = 1.0 ; /* if the Black Hole is inverted, it gives the impression of 'push- ing' the pattern away from its centre. otherwise it sucks. */ VScaleEq (Delta, Black_Hole->Inverted ? -Strength : Strength) ; /* add the scaled Delta to the input point to end up with TPoint. */ VAddEq (TPoint, Delta) ; } break; /* 10/23/1998 Talious added SPherical Cylindrical and toroidal warps */ case CYLINDRICAL_WARP: warp_cylindrical(TPoint, (CYLW *)Warp); break; case PLANAR_WARP: warp_planar(TPoint, (PLANARW *)Warp); break; case SPHERICAL_WARP: warp_spherical(TPoint, (SPHEREW *)Warp); break; case TOROIDAL_WARP: warp_toroidal(TPoint, (TOROIDAL *) Warp); break; default: Error("Warp type %d not yet implemented",Warp->Warp_Type); } Warp=Warp->Next_Warp; } for (i=X; i<=Z; i++) if (TPoint[i] > COORDINATE_LIMIT) TPoint[i]= COORDINATE_LIMIT; else if (TPoint[i] < -COORDINATE_LIMIT) TPoint[i] = -COORDINATE_LIMIT; }
int main (int argc, char **argv) { int nbObjects = 5000; int nbClasses = 20; SparseMatrix *objectObjectSimilarities = new SparseMatrix(nbObjects, nbObjects); // START generating test data printf ("Generating random test data...\n"); srandom(11); flt playFieldSize = 10; // Randomly positioning class prototypes, no variance for now: change // the playfield size to have classes more or less compactly packed // together flt *classCenters = new flt [2*nbClasses]; for (int i=0; i<nbClasses; i++) { classCenters[2*i] = FRAND(2*playFieldSize)-playFieldSize; classCenters[2*i+1] = FRAND(2*playFieldSize)-playFieldSize; } // Affecting a class to each object int *groundTruth = new int [nbObjects]; for (int i=0; i<nbObjects; i++) groundTruth[i] = random() % nbClasses; // Generating object coordinates ; they follow a normal gaussian // distribution around class centers flt *objectVectors = new flt [2*nbObjects]; for (int i=0; i<nbObjects; i++) twoGaussianRandoms (&(objectVectors[2*i]), &(objectVectors[2*i+1]), classCenters[2*groundTruth[i]], 1, classCenters[2*groundTruth[i]+1], 1); printf ("Computing test data similarities...\n"); // Computing similarities between objects flt *fullSimilarityMatrixLine = new flt[nbObjects]; for (int i=0; i<nbObjects; i++) { if (!(i%100)) { fprintf(stderr, "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b%i / %i", i, nbObjects); } for (int j=0; j<nbObjects; j++) fullSimilarityMatrixLine[j] = 1. / (1. + euclidDistance (objectVectors+2*i, objectVectors+2*j, 2)); objectObjectSimilarities->setLine(i, fullSimilarityMatrixLine, .3); } printf ("\nSimilarity matrix density : %5.2f %%\n\n", 100*objectObjectSimilarities->getDensity()); // END generating test data // Perform clustering test in all modes, writing out result and gnuplot commands const char* operationMode [] = {"br", "bc", "bo", "pr", "pc", "po", "Pr", "Pc", "Po", "mr", "mc", "mo", "Mr", "Mc", "Mo"}; int *result = new int [nbObjects]; int *init = new int [nbObjects]; for (int i=0; i<nbObjects; i++) init[i] = rand() % nbClasses; char *filename1 = new char[128]; char *filename2 = new char[128]; for (int mode=0; mode<15; mode++) { sprintf(filename1, "kaverages_classes_%s.txt", operationMode[mode]); sprintf(filename2, "kaverages_classes_%s.gnuplot", operationMode[mode]); kAveragesClustering (nbObjects, nbClasses, objectObjectSimilarities, result, operationMode[mode], 500, init); // Create two files to allow visualizing results with gnuplot FILE *fout = fopen(filename1, "w"); if (!fout) ERROR ("Could not open file '%s' for writing result.", filename1); for (int c=0; c<nbClasses; c++) fprintf(fout, "%f\t%f\n", classCenters[2*c], classCenters[2*c+1]); fprintf(fout, "\n\n"); for (int c=0; c<nbClasses; c++) { for (int i=0; i<nbObjects; i++) { if (result[i] == c) fprintf(fout, "%f\t%f\n", objectVectors[2*i], objectVectors[2*i+1]); } fprintf(fout, "\n\n"); } fclose(fout); fout = fopen(filename2, "w"); if (!fout) ERROR ("Could not open file '%s' for writing gnuplot commands.", filename2); fprintf(fout, "unset key;\nplot \"%s\" index 0 with points", filename1); for (int c=0; c<nbClasses; c++) { fprintf(fout, ", \"%s\" index %i with dots", filename1, c+1); } fprintf(fout, ";\n"); fclose(fout); fprintf(stderr, "\n\n----------------------------------------------\n\n"); } delete[] classCenters; delete[] groundTruth; delete[] objectVectors; delete[] fullSimilarityMatrixLine; delete[] result; delete objectObjectSimilarities; }
bool Bridge::UpdateEntityInTransit( Entity *_entity ) { Building *building = g_app->m_location->GetBuilding( m_nextBridgeId ); Bridge *nextBridge = (Bridge *) building; WorldObjectId id( _entity->m_id ); if( m_status > 0.0 && nextBridge && nextBridge->m_type == Building::TypeBridge && nextBridge->m_status > 0.0 ) { Matrix34 theirMat(nextBridge->m_front, g_upVector, nextBridge->m_pos); Matrix34 theirSignal = nextBridge->m_signal->GetWorldMatrix(theirMat); Vector3 offset = (theirSignal.pos - _entity->m_pos).Normalise(); double dist = ( _entity->m_pos - theirSignal.pos ).Mag(); bool arrived = false; _entity->m_vel = offset * BRIDGE_TRANSPORTSPEED; if( _entity->m_vel.Mag() * SERVER_ADVANCE_PERIOD > dist ) { _entity->m_vel = ( _entity->m_pos - theirSignal.pos ) / SERVER_ADVANCE_PERIOD; arrived = true; } _entity->m_pos += _entity->m_vel * SERVER_ADVANCE_PERIOD; _entity->m_onGround = false; _entity->m_enabled = false; if( arrived ) { // We are there if( nextBridge->m_bridgeType == Bridge::BridgeTypeEnd ) { Vector3 exitPos, exitFront; nextBridge->GetExit( exitPos, exitFront ); _entity->m_pos = exitPos; _entity->m_front = exitFront; _entity->m_enabled = true; _entity->m_onGround = true; _entity->m_vel.Zero(); g_app->m_location->m_entityGrid->AddObject( id, _entity->m_pos.x, _entity->m_pos.z, _entity->m_radius ); return true; } else if( nextBridge->m_bridgeType == Bridge::BridgeTypeTower ) { nextBridge->EnterTeleport( id, true ); return true; } } return false; } else { // Shit - we lost the carrier signal, so we die _entity->ChangeHealth( -500 ); _entity->m_enabled = true; _entity->m_vel = Vector3(SFRAND(10.0), FRAND(10.0), SFRAND(10.0) ); g_app->m_location->m_entityGrid->AddObject( id, _entity->m_pos.x, _entity->m_pos.z, _entity->m_radius ); return true; } }
static void do_rainbow(RAY *Ray, INTERSECTION *Intersection, COLOUR Colour) { int n; DBL dot, k, ki, index, x, y, l, angle, fade, f; VECTOR Temp; COLOUR Cr, Ct; RAINBOW *Rainbow; /* Why are we here. */ if (Frame.Rainbow == NULL) { return; } Make_ColourA(Ct, 0.0, 0.0, 0.0, 1.0, 1.0); n = 0; for (Rainbow = Frame.Rainbow; Rainbow != NULL; Rainbow = Rainbow->Next) { if ((Rainbow->Pigment != NULL) && (Rainbow->Distance != 0.0) && (Rainbow->Width != 0.0)) { /* Get angle between ray direction and rainbow's up vector. */ VDot(x, Ray->Direction, Rainbow->Right_Vector); VDot(y, Ray->Direction, Rainbow->Up_Vector); l = Sqr(x) + Sqr(y); if (l > 0.0) { l = sqrt(l); y /= l; } angle = fabs(acos(y)); if (angle <= Rainbow->Arc_Angle) { /* Get dot product between ray direction and antisolar vector. */ VDot(dot, Ray->Direction, Rainbow->Antisolar_Vector); if (dot >= 0.0) { /* Get index ([0;1]) into rainbow's colour map. */ index = (acos(dot) - Rainbow->Angle) / Rainbow->Width; /* Jitter index. */ if (Rainbow->Jitter > 0.0) { index += (2.0 * FRAND() - 1.0) * Rainbow->Jitter; } if ((index >= 0.0) && (index <= 1.0 - EPSILON)) { /* Get colour from rainbow's colour map. */ Make_Vector(Temp, index, 0.0, 0.0); Compute_Pigment(Cr, Rainbow->Pigment, Temp, Intersection); /* Get fading value for falloff. */ if ((Rainbow->Falloff_Width > 0.0) && (angle > Rainbow->Falloff_Angle)) { fade = (angle - Rainbow->Falloff_Angle) / Rainbow->Falloff_Width; fade = (3.0 - 2.0 * fade) * fade * fade; } else { fade = 0.0; } /* Get attenuation factor due to distance. */ k = exp(-Intersection->Depth / Rainbow->Distance); /* Colour's transm value is used as minimum attenuation value. */ k = max(k, fade * (1.0 - Cr[pTRANSM]) + Cr[pTRANSM]); /* Now interpolate the colours. */ ki = 1.0 - k; /* Attenuate filter value. */ f = Cr[pFILTER] * ki; Ct[pRED] += k * Colour[pRED] * ((1.0 - f) + f * Cr[pRED]) + ki * Cr[pRED]; Ct[pGREEN] += k * Colour[pGREEN] * ((1.0 - f) + f * Cr[pGREEN]) + ki * Cr[pGREEN]; Ct[pBLUE] += k * Colour[pBLUE] * ((1.0 - f) + f * Cr[pBLUE]) + ki * Cr[pBLUE]; Ct[pFILTER] *= k * Cr[pFILTER]; Ct[pTRANSM] *= k * Cr[pTRANSM]; n++; } } } } } if (n > 0) { COLC tmp = 1.0 / n; Colour[pRED] = Ct[pRED] * tmp; Colour[pGREEN] = Ct[pGREEN] * tmp; Colour[pBLUE] = Ct[pBLUE] * tmp; Colour[pFILTER] *= Ct[pFILTER]; Colour[pTRANSM] *= Ct[pTRANSM]; } }