void Physics_UpdateAttachPoint(Physics_AttachPoint* attachPoint, f32 mass, const vec3* gravity, f32 DTSq, f32 DTOverPreviousDT) { //Time-Corrected Verlet integration //http://archive.gamedev.net/archive/reference/articles/article2200.html vec3 xNeg1; CopyVec3(&xNeg1,&attachPoint->lastPosition); //Acceleration vec3 acceleration; ScaleVec3(&acceleration,&attachPoint->force,1.0f/mass); AddVec3_Self(&acceleration,gravity); vec3 x0; CopyVec3(&x0,&attachPoint->position); //Save current position into last position before updating new position CopyVec3(&attachPoint->lastPosition,&attachPoint->position); SubVec3(&attachPoint->position,&x0,&xNeg1); ScaleVec3_Self(&attachPoint->position,DTOverPreviousDT); AddVec3_Self(&attachPoint->position,&x0); AddScaledVec3_Self(&attachPoint->position,&acceleration,DTSq); }
void Physics_UpdateSpringNode(Physics_SpringNode* springNode, f32 restLength, f32 springK) { Physics_AttachPoint* attachPointA = springNode->pointA; Physics_AttachPoint* attachPointB = springNode->pointB; //Get stretch //The direction points from pointA to pointB vec3 stretch; SubVec3(&stretch,&attachPointB->position,&attachPointA->position); //Get magnitude of stretch const f32 stretchLength = TryNormalizeVec3_Self(&stretch); //Calculate force const f32 forceF = -springK * (stretchLength-restLength); //Add forces to attach points AddScaledVec3_Self(&attachPointA->force, &stretch, -forceF); AddScaledVec3_Self(&attachPointB->force, &stretch, forceF); }
void on_draw_frame(float az, float pch, float rll) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); static float yaws[SAMPLES]; static float pitches[SAMPLES]; static float rolls[SAMPLES]; static int index = -1; float y,p,r; float a=az, b=rll, c=pch; if (index<0) { int i; for (i=0;i<SAMPLES;++i) { yaws[i] = a; pitches[i] = b; rolls[i] = c; } index = 0; } else { yaws[index] = a; pitches[index] = b; rolls[index] = c; calcMovingAvg(yaws, &y); calcMovingAvg(pitches, &p); calcMovingAvg(rolls, &r); index = (index+1)%SAMPLES; } Vector3 eye; eye.x = 0.f; eye.y = 0.f; eye.z = -8.f; //Transform original = Rotate; y = (y); p = -(p + 3.14159f); r = (r); Transform rot; Transform yaw = RotateY(y); Transform pitch = RotateX(p); //Transform roll = RotateZ(r); //TransformTrans(&pitch, &original, &rot); rot = yaw; TransformTrans(&pitch, &rot, &rot); //TransformTrans(&roll, &rot, &rot); Vector3 center; center.x = 0.f; center.y = 0.f; center.z = 0.f; Vector3 temp; SubVec3(¢er, &eye, &temp); TransformVec3(&rot, &temp, &temp); AddVec3(&eye, &temp, ¢er); Vector3 up; up.x = 0.f; up.y = 1.f; up.z = 0.f; TransformVec3(&rot, &up, &up); NormalizeVec3(&up, &up); MakeLookAtTrans(&eye, ¢er, &up, &View); __android_log_print(ANDROID_LOG_INFO, "NATIVE", "Z POSITION %f %f %f", y, p, r); __android_log_print(ANDROID_LOG_INFO, "NATIVE", "Z LOOK %f %f %f", center.x, center.y, center.z); __android_log_print(ANDROID_LOG_INFO, "NATIVE", "Z UP %f %f %f", up.x, up.y, up.z); TransformTrans(&Projection, &View, &ViewProjection); SetupDragonTrans(); DrawDragon(); }
void SampleGame2D::UpdateMainGame(f32 timeElapsed) { //Always update GUI first MyObjectFactories_UpdateGUI(timeElapsed); if(m_paused == true) { //If it's paused, just do a quick game update and bust out Game::Update(timeElapsed); return; } const f32 maxTimeElapsed = 1.0f/30.0f; if(timeElapsed > maxTimeElapsed) { timeElapsed = maxTimeElapsed; } switch(m_camState) { case CamState_Normal: { ConstrainCameraToTiledLevel(); break; } } mat4f_SetLookAtPos(m_view,&m_camPos); //update and debug draw joystick for(u32 i=0; i<GAME_MAX_PLAYERS; ++i) { #if defined (PLATFORM_IOS) || defined (PLATFORM_ANDROID) m_pad[i].Update(); m_pad[i].DebugDraw(); #endif #if defined (PLATFORM_OSX) || defined (PLATFORM_WIN) //TODO: some XBOX controller stuff here #endif } //TODO: update player //Update all the object factories (which in turn updates each object) MyObjectFactories_Update(timeElapsed); CoreUIView* pTitleView = (CoreUIView*)COREOBJECTMANAGER->GetObjectByHandle(m_hTitleScreen); if(pTitleView != NULL) { switch(m_titleScreenState) { case TitleScreenState_WaitingToFadeOut: { m_timeScreenTimer += timeElapsed*2.0f; if(m_timeScreenTimer > 2.0f) { m_titleScreenState = TitleScreenState_FadingOut; } break; } case TitleScreenState_FadingOut: { pTitleView->fadeAlpha -= timeElapsed; if(pTitleView->fadeAlpha <= 0.0f) { pTitleView->fadeAlpha = 0.0f; m_titleScreenState = TitleScreenState_Idle; } pTitleView->LayoutView(); break; } case TitleScreenState_Idle: { break; } } } //Only update the direction if the normalize doesn't fail vec3 tempCamDir; SubVec3(&tempCamDir, &m_camPos, &m_lastCamPos); //TODO: care what direction the camera is going m_lastCamPos = m_camPos; }