///========================================================================================================================================== /// Debris ///========================================================================================================================================== void DebrisEmitter::Update(double deltaSeconds){ double currentTime = GetCurrentSeconds(); //update existing particles for (Particles::iterator particleIter = m_particles.begin(); particleIter != m_particles.end();){ Particle* particle = *particleIter; //delete expired particles if (particle->m_expirationTime < currentTime){ delete particle; particleIter = m_particles.erase(particleIter); continue; } particle->m_velocity.y -= m_acceleration * (float)deltaSeconds; particle->Update(deltaSeconds); if (particle->m_position.y < m_position.y){ particle->m_velocity.y = -particle->m_velocity.y * GetRandomFloatInRange(0.6f, 0.8f); } ++particleIter; } //add new particles m_timeSinceParticleEmission += deltaSeconds; if (m_timeSinceParticleEmission >= m_timeBetweenParticleEmissions){ AddParticles(); m_timeSinceParticleEmission -= m_timeBetweenParticleEmissions; } }
///===================================================== /// ///===================================================== void ParticleEmitter::Update(double deltaSeconds){ double currentTime = GetCurrentSeconds(); //update existing particles for (Particles::iterator particleIter = m_particles.begin(); particleIter != m_particles.end();){ Particle* particle = *particleIter; //delete expired particles if (particle->m_expirationTime < currentTime){ delete particle; particleIter = m_particles.erase(particleIter); continue; } particle->Update(deltaSeconds); ++particleIter; } //add new particles m_timeSinceParticleEmission += deltaSeconds; if (m_timeSinceParticleEmission >= m_timeBetweenParticleEmissions){ AddParticles(); m_timeSinceParticleEmission -= m_timeBetweenParticleEmissions; } }
void TheApp::Update() { static double lastTime = GetCurrentSeconds(); double currentTime = GetCurrentSeconds(); double deltaSeconds = currentTime - lastTime; lastTime = currentTime; if(deltaSeconds > 0.5) { deltaSeconds = 0.5; } //system clock GetSystemClock().AdvanceTime(deltaSeconds); deltaSeconds = GetSystemClock().GetChild("GameClock")->GetDeltaSeconds(); if(m_world) { m_world->Update(deltaSeconds ); } if (m_jobManager) { m_jobManager->Update(); } if (m_networkSystem) { m_networkSystem->Update(); } //update debug shapes if (m_renderer) { g_debugShapes.Update(deltaSeconds); } // if(m_soundSystem){ // m_soundSystem->Update(); // } if (m_textSystem) { m_textSystem->Update(deltaSeconds); } if (m_particleSystem) { m_particleSystem->Update(deltaSeconds); } }
//------------------------------------------------------------------------------ // // void CMiniGameCatchLayer::AddCartchSprite(CCPoint p) { //UXLOG(L"Add sprite %0.2f x %02.f",p.x,p.y); CCSpriteBatchNode *mgr = (CCSpriteBatchNode*)getChildByTag( kTagSpriteManager ); int index = 1 + (int)(CCRANDOM_0_1() * 11); int pixScale = 1; #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) if( CDevice::GetDeviceType() == "iPad" ) { pixScale = 2; } #endif CCRect rect( ( index % 4) * 32 * pixScale, index / 4 * 32 * pixScale ,32 * pixScale, 32 * pixScale ); CCSprite *sprite = CCSprite::spriteWithBatchNode( mgr, rect ); // addChild(sprite, MINIGAME_LAYER_BEGIN); sprite->setPosition( CCPointMake( p.x, p.y) ); sprite->setTag( index ); mgr->addChild( sprite ); //sprite->setAnchorPoint(ccp( 0,0 )); // Define the dynamic body. //Set up a 1m squared box in the physics world b2BodyDef bodyDef; bodyDef.type = b2_dynamicBody; bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO); bodyDef.userData = sprite; b2Body *body = world->CreateBody(&bodyDef); // Define another box shape for our dynamic body. b2PolygonShape dynamicBox; dynamicBox.SetAsBox(32.0f/ PTM_RATIO / 2.0f, 32.0f/ PTM_RATIO / 2.0f);//These are mid points for our 1m box // Define the dynamic body fixture. b2FixtureDef fixtureDef; fixtureDef.shape = &dynamicBox; fixtureDef.density = 5.0f; fixtureDef.friction = 0.3f; body->CreateFixture(&fixtureDef); CCSize screenSize = CCDirector::sharedDirector()->getWinSize(); b2Vec2 d; srand( GetCurrentSeconds() ); d.Set( rand() % 10 * screenSize.width / 10.f, screenSize.height + screenSize.height ); d.Normalize(); b2Vec2 F = 80 * d; body->ApplyForce(F,d); }
///===================================================== /// Splash ///===================================================== void WaveEmitter::AddParticles(){ double currentTime = GetCurrentSeconds(); const float maxParticleVelocity = m_radius / (float)m_duration; for (int particleCount = 0; particleCount < m_particlesPerEmission; ++particleCount){ float angle1 = GetRandomFloatInRange(0.0f, 2.0f * PI); float magnitude = GetRandomFloatInRange(maxParticleVelocity * 0.8f, maxParticleVelocity); const Vec3 velocity(sin(angle1), 0.0f, cos(angle1)); m_particles.push_back(new Particle(m_position, magnitude * velocity, currentTime + GetRandomDoubleInRange(m_duration * 0.9, m_duration), m_color)); } }
///========================================================================================================================================== /// Smoke and Fire ///========================================================================================================================================== void SmokeEmitter::AddParticles(){ double currentTime = GetCurrentSeconds(); for (int particleCount = 0; particleCount < m_particlesPerEmission; ++particleCount){ float angle1 = GetRandomFloatInRange(0.0f, 2.0f * PI); float angle2 = GetRandomFloatInRange(0.0f, m_normalizedDensity * PI); float sinAngle2 = sin(angle2); const Vec3 velocity(sinAngle2 * sin(angle1), cos(angle2), sinAngle2 * cos(angle1)); m_particles.push_back(new Particle(m_position, m_speed * velocity, currentTime + GetRandomDoubleInRange(m_duration * 0.5, m_duration), m_color)); } }
void NetworkSession::UpdateIncomingPacketOnSocket(NetPacket* sockPacket) { //do something with sockPacket if (sockPacket->IsValid()) { //ConsolePrintString("packet validated!"); //ConsolePrintString("\n" + sockPacket->ToStringAsBase(16)); NetAddress* sockPacketAddr = sockPacket->GetAddress(); if (sockPacketAddr) { NetSender packetSender; packetSender.addr = sockPacketAddr; //grab the name in the map packetSender.conn = FindConnectionInMap(sockPacket->GetConnIndex()); packetSender.session = (NetSession*)this; std::string packetSenderConnName = GetNetConnectionMapKey(*sockPacket->GetAddress(), sockPacket->GetConnIndex()); //packetSenderConnName = GetConnNameFromIndex(sockPacket->GetConnIndex()); if (packetSender.conn) { packetSenderConnName = packetSender.conn->GetName(); } std::string packetRecievedString = "\n===Received NetPacket=== from [" + packetSenderConnName + "] =>"; packetRecievedString += "packet[" + sockPacket->ToStringAsBase(16) + "]"; if (packetSender.conn) { packetSender.conn->m_timeRecievedPacket = GetCurrentSeconds(); //packetRecievedString += " => Recv Packet Time = " + DoubleToString(packetSender.conn->m_timeRecievedPacket); } //this is the most spam //ConsolePrintString(packetRecievedString); ExecuteMessageCallbacksFromSender(&packetSender, sockPacket); // //find connection on that addr // NetConnection* netConnForPacket = FindConnectionInMap(*sockPacketAddr); // //bool trackAck = false; // if (netConnForPacket) { // //break packet out into it's messages and add them to the connections // //ConsolePrintString("\ninConnection!!"); // // netConnForPacket->m_timeRecievedPacket = GetCurrentSeconds(); // // ExecuteMessageCallbacksFromSender(netConnForPacket, sockPacket); // // } } }//end of nested if }
///===================================================== /// ///===================================================== void WaveEmitter::Update(double deltaSeconds){ double currentTime = GetCurrentSeconds(); //update existing particles for (Particles::iterator particleIter = m_particles.begin(); particleIter != m_particles.end();){ Particle* particle = *particleIter; //delete expired particles if (particle->m_expirationTime < currentTime){ delete particle; particleIter = m_particles.erase(particleIter); continue; } //COLORFUL SPIRAL LOL Vec2 tempVel(particle->m_velocity.x, particle->m_velocity.z); tempVel.RotateDegrees(32.0f * (float)deltaSeconds * 90.0f / (float)m_duration); particle->m_velocity = Vec3(tempVel.x, 0.0f, tempVel.y); Vec2 tempPos(particle->m_position.x, particle->m_position.z); tempPos.RotateDegrees(32.0f * (float)deltaSeconds * 90.0f / (float)m_duration); particle->m_position = Vec3(tempPos.x, particle->m_position.y, tempPos.y); if (GetRandomFloatInRange(0.0f, 1.0f) > cos(currentTime)){ particle->m_color.r -= (unsigned char)GetRandomIntInRange(0, 3); particle->m_color.g -= (unsigned char)GetRandomIntInRange(2, 6); particle->m_color.b += (unsigned char)GetRandomIntInRange(1, 9); } particle->m_position.y = m_position.y + m_waveHeight * cos((CalcDistance(Vec2(m_position.x, m_position.z), Vec2(particle->m_position.x, particle->m_position.z)) - (float)currentTime) * m_waveSpeed); particle->Update(deltaSeconds); ++particleIter; } //add new particles m_timeSinceParticleEmission += deltaSeconds; if (m_timeSinceParticleEmission >= m_timeBetweenParticleEmissions){ AddParticles(); m_timeSinceParticleEmission -= m_timeBetweenParticleEmissions; } }
///===================================================== /// ///===================================================== Bullet::Bullet(const Vec2& position, float shipOrientation, const OpenGLRenderer& renderer, EngineAndrew::Material& material) : GameEntity(position, renderer, material), m_spawnTime(GetCurrentSeconds()){ Vertex_Anim v0(Vec3(0.0f, -0.6f, 0.0f)); Vertex_Anim v1(Vec3(0.6f, 0.0f, 0.0f)); Vertex_Anim v2(Vec3(0.0f, 0.6f, 0.0f)); Vertex_Anim v3(Vec3(-0.6f, 0.0f, 0.0f)); m_mesh.m_vertices.push_back(v0); m_mesh.m_vertices.push_back(v1); m_mesh.m_vertices.push_back(v2); m_mesh.m_vertices.push_back(v3); m_physics.m_orientationDegrees = shipOrientation; m_physics.m_velocity.SetLengthAndHeadingDegrees(300.0f, shipOrientation); m_radius = 0.6f; m_mesh.UseDefaultIndeces(); m_mesh.SendVertexDataToBuffer(&renderer); }
//constructors BaseGameplayMetric() : timestamp(GetCurrentSeconds()) { //do nothing }
ReliableTracker(const uint16_t& packetAck, size_t count) : timeCreated(GetCurrentSeconds()), packetAckID(packetAck) { reliableIDs.reserve(count); }
///===================================================== /// ///===================================================== void ParticleEmitter::AddParticles(){ double currentTime = GetCurrentSeconds(); for (int particleCount = 0; particleCount < m_particlesPerEmission; ++particleCount){ m_particles.push_back(new Particle(m_position, Vec3(0.0f, 1.0f, 0.0f), currentTime + m_duration, m_color)); } }
//#define collisions ///========================================================================================================================================== /// Fireworks ///========================================================================================================================================== void FireworksEmitter::Update(double deltaSeconds){ double currentTime = GetCurrentSeconds(); #if defined collisions Vec3s initialPositions; Vec3s finalPositions; #endif //update existing particles for (Particles::iterator particleIter = m_particles.begin(); particleIter != m_particles.end();){ Particle* particle = *particleIter; //delete expired particles if (particle->m_expirationTime < currentTime){ delete particle; particleIter = m_particles.erase(particleIter); continue; } #if defined collisions initialPositions.push_back(particle->m_position); #endif particle->m_velocity.y -= m_acceleration * (float)deltaSeconds; particle->Update(deltaSeconds); #if defined collisions finalPositions.push_back(particle->m_position); #endif ++particleIter; } #if defined collisions int count1 = 0; int count2 = 0; for (Particles::iterator particleIter = m_particles.begin(); particleIter != m_particles.end();){ Particle* particle = *particleIter; Vec3 p1 = initialPositions.at(count1); if (CalcDistanceSquared(p1, m_position) < 0.5f){ ++particleIter; ++count1; continue; } Vec3 p2 = finalPositions.at(count1); count2 = ++count1; float R1 = 0.00000001f; for (Particles::iterator particleIter2 = ++particleIter; particleIter2 != m_particles.end(); ++particleIter2, ++count2){ Particle* particle2 = *particleIter2; Vec3 q1 = initialPositions.at(count2); if (CalcDistanceSquared(q1, m_position) < 0.5f){ continue; } Vec3 q2 = finalPositions.at(count2); float R2 = 0.00000001f; Vec3 x0(q1 - p1); Vec3 e(q2 - q1 - (p2 - p1)); float R = R1 + R2; float collisionTest = (DotProduct(e, x0) * DotProduct(e, x0)) - (DotProduct(e, e) * (DotProduct(x0, x0) - (R * R))); if (collisionTest >= 0.0f){ //did collide float collisionTime = (-DotProduct(e, x0) - sqrt(collisionTest)) / DotProduct(e, e); if (collisionTime < 0.0f) collisionTime = 0.0f; particle2->m_color = RGBAchars::YELLOW; particle->m_color = RGBAchars::YELLOW; particle->m_position = initialPositions.at(count1 - 1); particle2->m_position = initialPositions.at(count2); Vec3 collisionNormal = particle->m_position - particle2->m_position; float length = collisionNormal.Normalize(); if (length <= 0.001f) continue; float v1Parallel = DotProduct(particle->m_velocity, collisionNormal); Vec3 v1Perp = particle->m_velocity - v1Parallel * collisionNormal; float v2Parallel = DotProduct(particle2->m_velocity, collisionNormal); Vec3 v2Perp = particle2->m_velocity - v2Parallel * collisionNormal; float e = 0.7f; Vec3 v1ParralelFinal = (0.5f * (v1Parallel + v2Parallel + e * (v2Parallel - v1Parallel))) * collisionNormal; Vec3 v2ParralelFinal = (0.5f * (v1Parallel + v2Parallel - e * (v2Parallel - v1Parallel))) * collisionNormal; particle->m_velocity = v1ParralelFinal + v1Perp; particle2->m_velocity = v2ParralelFinal + v2Perp; } } } #endif //add new particles m_timeSinceParticleEmission += deltaSeconds; if (m_timeSinceParticleEmission >= m_timeBetweenParticleEmissions){ AddParticles(); m_timeSinceParticleEmission -= m_timeBetweenParticleEmissions; } }
DWORD WINAPI SensorFusionProc(LPVOID lpParam) { #define MAGNET_VELOCITY 1 float dT = 0.05f, fs = 1.0f/dT, mfs = fs; float ax, ay, az; float mx, my, mz; float gx, gy, gz; #ifndef MAGNET_VELOCITY Rotation *rotation = NULL; ISTBOOL rotation_done = ISTFALSE; #endif Magnet *magnet = NULL; ISTBOOL magnet_done = ISTFALSE; ISTFLOAT ws[3] = {0}; ISTFLOAT gs[3] = {0}; ISTFLOAT gdata[3]; ISTFLOAT mdata[3]; CSensorBase *accel_sensor = NULL; CSensorBase *magnet_sensor = NULL; CSensorBase *gyro_sensor = NULL; AccelerationCalibration *accel_cal = NULL; AcceleratorMeasurement cx, cy, cz; float g; float tmA, tmM; DWORD dwRet, dwCnt, dwStart, dwEnd, dwTimeout; DWORD samples = (DWORD)(fs/mfs); DWORD period = (DWORD)(1000.0f/fs); HANDLE event = NULL; IST_DBG("+++ Enter SensorFusionProc() +++\n"); FreezeGUI(false); accel_sensor = CreateSensor(DEV_ID_KIONIX, fs, g_pMain->m_iSenI2c); if (!accel_sensor) { IST_DBG("!accel_sensor\n"); goto EXIT; } magnet_sensor = CreateSensor(DEV_ID_ISENTEK_8303, mfs, g_pMain->m_iSenI2c); if (!magnet_sensor) { IST_DBG("!magnet_sensor\n"); goto EXIT; } gyro_sensor = CreateSensor(DEV_ID_LG3GYRO, fs, g_pMain->m_iSenI2c); if (!gyro_sensor) { IST_DBG("!gyro_sensor\n"); goto EXIT; } magnet = IST_Magnet()->New(); if (!magnet) { IST_DBG("!magnet\n"); goto EXIT; } #ifndef MAGNET_VELOCITY rotation = IST_Rotation()->New(); if (!rotation) { IST_DBG("!rotation\n"); goto EXIT; } #endif #define GRAVITY_SENSOR_POSITIVE_X_OFFSET 12.600f #define GRAVITY_SENSOR_NEGATIVE_X_OFFSET -8.450f #define GRAVITY_SENSOR_ZERO_X_OFFSET 1.838f #define GRAVITY_SENSOR_POSITIVE_Y_OFFSET 11.350f #define GRAVITY_SENSOR_NEGATIVE_Y_OFFSET -9.365f #define GRAVITY_SENSOR_ZERO_Y_OFFSET 0.981f #define GRAVITY_SENSOR_POSITIVE_Z_OFFSET 14.920f #define GRAVITY_SENSOR_NEGATIVE_Z_OFFSET -7.000f #define GRAVITY_SENSOR_ZERO_Z_OFFSET 3.154f #define GRAVITY_SENSOR_G 9.81f g = GRAVITY_SENSOR_G; cx.at_positive_gravity = GRAVITY_SENSOR_POSITIVE_X_OFFSET; cx.at_negative_gravity = GRAVITY_SENSOR_NEGATIVE_X_OFFSET; cx.at_zero_gravity = GRAVITY_SENSOR_ZERO_X_OFFSET; cy.at_positive_gravity = GRAVITY_SENSOR_POSITIVE_Y_OFFSET; cy.at_negative_gravity = GRAVITY_SENSOR_NEGATIVE_Y_OFFSET; cy.at_zero_gravity = GRAVITY_SENSOR_ZERO_Y_OFFSET; cz.at_positive_gravity = GRAVITY_SENSOR_POSITIVE_Z_OFFSET; cz.at_negative_gravity = GRAVITY_SENSOR_NEGATIVE_Z_OFFSET; cz.at_zero_gravity = GRAVITY_SENSOR_ZERO_Z_OFFSET; accel_cal = CAccelerationCalibration()->New(g, cx, cy, cz); if (!accel_cal) { IST_DBG("!accel_cal\n"); goto EXIT; } event = CreateEvent(NULL, TRUE, FALSE, NULL); if (!event) { goto EXIT; } dwCnt = 0; tmM = tmA = GetCurrentSeconds(); dwTimeout = period; while (!g_pMain->bStopSignal) { xIST_DBG("dwTimeout = %u ms\n", dwTimeout); dwRet = WaitForSingleObject(event, dwTimeout); if (WAIT_TIMEOUT != dwRet) { xIST_DBG("dwRet = %u\n", dwRet); g_pMain->bStopSignal = true; continue; } dwStart = GetTickCount(); /* get real gyroscope data */ if (FALSE == ReadSensorData(gyro_sensor, &gx, &gy, &gz, CSensorBase::GYRO_UNITS::CONFIG_GYRO_UNITS_RAD)){ g_pMain->bStopSignal = true; continue; } gs[0] = FloatType(gx); gs[1] = FloatType(gy); gs[2] = FloatType(gz); /* Get accelerator data */ if (FALSE == ReadSensorData(accel_sensor, &ax, &ay, &az, CSensorBase::ACCEL_UNITS::CONFIG_ACCEL_UNITS_MPSS)){ g_pMain->bStopSignal = true; continue; } if (accel_cal->Process(accel_cal, ax, ay, az, dT)) { accel_cal->GetData(accel_cal, &ax, &ay, &az); } gdata[0] = FloatType(ax); gdata[1] = FloatType(ay); gdata[2] = FloatType(az); xIST_DBG("a[%14.10f,%14.10f,%14.10f]\n", ax, ay, az); /* Get magnet field data */ if (FALSE == ReadSensorData(magnet_sensor, &mx, &my, &mz, CSensorBase::MAG_UNITS::CONFIG_MAG_UNITS_UT)){ g_pMain->bStopSignal = true; continue; } dT = EllapsedSeconds(&tmM); mdata[0] = FloatType(mx); mdata[1] = FloatType(my); mdata[2] = FloatType(mz); xIST_DBG("m[%14.10f,%14.10f,%14.10f]\n", mx, my, mz); #ifndef MAGNET_VELOCITY rotation_done = rotation->Process(rotation, mdata, gdata, FloatType(dT)); #endif magnet_done = magnet->Process(magnet, mdata, FloatType(dT)); #ifndef MAGNET_VELOCITY /* check valid */ xIST_DBG("rotation_done:%s\n", rotation_done ? "YES" : "NO"); #endif xIST_DBG("magnet_done:%s\n", magnet_done ? "YES" : "NO"); #ifndef MAGNET_VELOCITY if (rotation_done && magnet_done) { #else if (magnet_done) { #endif CString tmpStr; if (g_pMain->GetCurrentPage() == CiSensorsDlg::GYROSCOPE_DATA) { xIST_DBG("update gyro\n"); #ifdef MAGNET_VELOCITY ws[0] = magnet->Velocity[0]; ws[1] = magnet->Velocity[1]; ws[2] = magnet->Velocity[2]; #else ws[0] = rotation->Velocity[0]; ws[1] = rotation->Velocity[1]; ws[2] = rotation->Velocity[2]; #endif xIST_DBG("%14.10f,%14.10f,%14.10f,%14.10f,%14.10f,%14.10f\n", gx, gy, gz, (float)NativeType(ws[0]), (float)NativeType(ws[1]), (float)NativeType(ws[2])); for (int i = 0; i < 3; ++i) { tmpStr.Format(_T("%0.2f"), (float)NativeType(ws[i])); g_pMain->m_SubDlgGyroscopeData.GetDlgItem(IDC_STATIC_EGYROSCOPE_X + i)->SetWindowTextW(tmpStr); tmpStr.Format(_T("%0.2f"), (float)NativeType(gs[i])); g_pMain->m_SubDlgGyroscopeData.GetDlgItem(IDC_STATIC_GYROSCOPE_X + i)->SetWindowTextW(tmpStr); g_pMain->m_SubDlgGyroscopeData.m_GyroWaveform[i].SetData( (float)NativeType(gs[0]), (float)NativeType(gs[1]), (float)NativeType(gs[2])); g_pMain->m_SubDlgGyroscopeData.m_SoftGyroWaveform[i].SetData( (float)NativeType(ws[0]), (float)NativeType(ws[1]), (float)NativeType(ws[2])); } } } dwEnd = GetTickCount(); if (period > (dwEnd - dwStart)) dwTimeout = period - (dwEnd - dwStart); else dwTimeout = 0; } EXIT: if (event) { CloseHandle(event); event = NULL; } #ifndef MAGNET_VELOCITY if (rotation) { IST_Rotation()->Delete(rotation); rotation = NULL; } #endif if (magnet) { IST_Magnet()->Delete(magnet); magnet = NULL; } DestroySensor(&gyro_sensor); DestroySensor(&magnet_sensor); DestroySensor(&accel_sensor); FreezeGUI(true); IST_DBG("--- Leave SensorFusionProc() ---\n"); ExitThread(0); }
float EllapsedSeconds(float *start) { float n = GetCurrentSeconds(), s = start ? *start : 0.0f; start ? *start = n : (void)0; return n - s; }