AxisPhysicsModel::Derivative AxisPhysicsModel::Evaluate(const State &aInitState, double aDeltaTime, const Derivative &aDerivative) { State state( aInitState.p + aDerivative.dp*aDeltaTime, aInitState.v + aDerivative.dv*aDeltaTime ); return Derivative( state.v, Acceleration(state) ); }
void CAccelerometer::getAccelValues(Acceleration& acc) { int64 timestamp = s3eTimerGetUTC(); // Cap and scale to [-1,1] double x = ((double) M(s3eAccelerometerGetX)()) / 1000.0f; double y = ((double) M(s3eAccelerometerGetY)()) / 1000.0f; double z = ((double) M(s3eAccelerometerGetZ)()) / 1000.0f; x = x <- 1 ? -1 : x; x = x > 1 ? 1 : x; y = y <- 1 ? -1 : y; y = y > 1 ? 1 : y; z = z <- 1 ? -1 : z; z = z > 1 ? 1 : z; acc = Acceleration(x, y, z, timestamp); }
FVector UProjectileMovementComponent::ComputeAcceleration(const FVector& InVelocity, float DeltaTime) const { FVector Acceleration(FVector::ZeroVector); Acceleration.Z += GetEffectiveGravityZ(); if (bIsHomingProjectile && HomingTargetComponent.IsValid()) { Acceleration += ComputeHomingAcceleration(InVelocity, DeltaTime); } return Acceleration; }
void BaseCollisionPhysicsObject::integrate(CollisionNode* Node) { if (!Node) return; const f32 dt = io::Timer::getGlobalSpeed(); /* Acceleration is force / mass */ dim::vector3df Acceleration(Gravity_); Acceleration += Force_; Acceleration /= Mass_; /* Apply velocity and forces */ Node->translate(Velocity_ * dt); Velocity_ += (Acceleration / Mass_);// * dt; /* Reset forces */ Force_ = 0.0f; }
int main(int argc, char const *argv[]) { MLV_Keyboard_button sym=MLV_KEYBOARD_NONE; MLV_Button_state state; MLV_Event event = MLV_NONE; int quit=0,i=1, flag_tir=0; int testX=0; int testY=rand() %650; int ennemy_x[4]={50, 240, 430, 620}; MLV_create_window("Test", "test", 710, 710); draw_window(testX, testY); t_elem Vaisseau; t_elem ennemy; t_elem Missile; t_elem Missile2; create_vaisseau(&Vaisseau); create_ennemi(&ennemy); create_missile(&Missile); create_missile(&Missile2); draw(&Vaisseau); srand(time(NULL)); //MLV_change_frame_rate( 500 ); // draw(&ennemy); do { event = MLV_get_event( &sym, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &state); switch( event ){ case MLV_NONE : // Aucun évènement récupéré /*ennemy.y = ennemy.y+5; if(ennemy.y > 700){ ennemy.y = -100; ennemy.x = ennemy_x[rand() %4]; } */ /*if(MLV_get_time() == 3000) { draw(&ennemy); }*/ testX += vitesse_defilement; // Affichage étoile if(testX > 700){ testX=0; testY=rand() %635; } if(flag_tir==2 ) // Tir 2 { Missile.y=Missile.y-20; Missile2.y=Missile2.y-20; if(Missile.y>0) draw(&Missile); if(Missile2.y>0) draw(&Missile2); if(Missile2.y<-30) { flag_tir=0; Missile.y=600; Missile2.y=600; // Faire une fonction reset qui prend pos collision en param (quand on ferra la gestion des collisions ) } } if(flag_tir==1) // Tir 1 { Missile.y=Missile.y-20; draw(&Missile); if(Missile.y<-30) { flag_tir=0; Missile.y=600; // Faire une fonction reset qui prend pos collision en param (quand on ferra la gestion des collisions ) } } draw_window(testX, testY); // draw(&ennemy); draw(&Vaisseau); break; case MLV_KEY : while((Vaisseau.x < 650 && MLV_get_keyboard_state( MLV_KEYBOARD_RIGHT ) == MLV_PRESSED )) // Bouge à droite { if(MLV_get_keyboard_state( MLV_KEYBOARD_SPACE ) == MLV_PRESSED) { if(flag_tir == 0){ Missile.x = Vaisseau.x; flag_tir=1; } } i++; Acceleration(&Vaisseau, i, 1); testX += vitesse_defilement; if(testX > 700){ testX=0; testY=rand() %635; } if(flag_tir==1) { Missile.y=Missile.y-20; draw(&Missile); if(Missile.y<-30) { flag_tir=0; Missile.y=600; } } draw_window(testX, testY); draw(&Vaisseau); } while( MLV_get_keyboard_state( MLV_KEYBOARD_LEFT ) == MLV_PRESSED && Vaisseau.x > 20) // Bouge à gauche { if(MLV_get_keyboard_state( MLV_KEYBOARD_SPACE ) == MLV_PRESSED) { if(flag_tir==0) Missile.x = Vaisseau.x; flag_tir=1; } i++; Acceleration(&Vaisseau, i, -1); testX += vitesse_defilement; if(testX > 700){ testX=0; testY=rand() %635; } if(flag_tir==1) { Missile.y=Missile.y-20; draw(&Missile); if(Missile.y<-30) { flag_tir=0; Missile.y=600; } } draw_window(testX, testY); draw(&Vaisseau); } if( MLV_get_keyboard_state( MLV_KEYBOARD_SPACE ) == MLV_PRESSED ){ // Tir Missile.x = Vaisseau.x+17; Missile2.x = Vaisseau.x+17; if(flag_tir == 1) { flag_tir = 2; } if(flag_tir == 0) { flag_tir = 1; } } default : fprintf(stderr,"Erreur : la valeur de l'évènement récupéré est impossible."); } i=0; } while( ! quit ); MLV_wait_seconds(10); return 0; }
void FWiimoteInputDevice::handle_event(struct wiimote_t* wm, int id) { UE_LOG(LogWiimote, Log, TEXT("\n\n--- EVENT [id %i] ---"), wm->unid); CurrentStates[0] = IS_PRESSED(wm, WIIMOTE_BUTTON_A) || IS_HELD(wm, WIIMOTE_BUTTON_A); CurrentStates[1] = IS_PRESSED(wm, WIIMOTE_BUTTON_B) || IS_HELD(wm, WIIMOTE_BUTTON_B); CurrentStates[2] = IS_PRESSED(wm, WIIMOTE_BUTTON_ONE) || IS_HELD(wm, WIIMOTE_BUTTON_ONE); CurrentStates[3] = IS_PRESSED(wm, WIIMOTE_BUTTON_TWO) || IS_HELD(wm, WIIMOTE_BUTTON_TWO); CurrentStates[4] = IS_PRESSED(wm, WIIMOTE_BUTTON_PLUS) || IS_HELD(wm, WIIMOTE_BUTTON_PLUS); CurrentStates[5] = IS_PRESSED(wm, WIIMOTE_BUTTON_MINUS) || IS_HELD(wm, WIIMOTE_BUTTON_MINUS); CurrentStates[6] = IS_PRESSED(wm, WIIMOTE_BUTTON_UP) || IS_HELD(wm, WIIMOTE_BUTTON_UP); CurrentStates[7] = IS_PRESSED(wm, WIIMOTE_BUTTON_DOWN) || IS_HELD(wm, WIIMOTE_BUTTON_DOWN); CurrentStates[8] = IS_PRESSED(wm, WIIMOTE_BUTTON_LEFT) || IS_HELD(wm, WIIMOTE_BUTTON_LEFT); CurrentStates[9] = IS_PRESSED(wm, WIIMOTE_BUTTON_RIGHT) || IS_HELD(wm, WIIMOTE_BUTTON_RIGHT); if (IS_PRESSED(wm, WIIMOTE_BUTTON_HOME)) { UE_LOG(LogWiimote, Log, TEXT("HOME pressed")); } /* show events specific to supported expansions */ if (wm->exp.type == EXP_NUNCHUK || wm->exp.type == EXP_MOTION_PLUS_NUNCHUK) { /* nunchuk */ struct nunchuk_t* nc = (nunchuk_t*)&wm->exp.nunchuk; const float DEADZONE = 0.6f; CurrentStates[10] = IS_PRESSED(nc, NUNCHUK_BUTTON_C) || IS_HELD(wm, NUNCHUK_BUTTON_C); CurrentStates[11] = IS_PRESSED(nc, NUNCHUK_BUTTON_Z) || IS_HELD(wm, NUNCHUK_BUTTON_Z); CurrentStates[12] = nc->js.y > DEADZONE; CurrentStates[13] = nc->js.y < -DEADZONE; CurrentStates[14] = nc->js.x < -DEADZONE; CurrentStates[15] = nc->js.x > DEADZONE; UE_LOG(LogWiimote, Log, TEXT("nunchuk roll = %f"), nc->orient.roll); UE_LOG(LogWiimote, Log, TEXT("nunchuk pitch = %f"), nc->orient.pitch); UE_LOG(LogWiimote, Log, TEXT("nunchuk yaw = %f"), nc->orient.yaw); UE_LOG(LogWiimote, Log, TEXT("nunchuk joystick angle: %f"), nc->js.ang); UE_LOG(LogWiimote, Log, TEXT("nunchuk joystick magnitude: %f"), nc->js.mag); UE_LOG(LogWiimote, Log, TEXT("nunchuk joystick vals: %f, %f"), nc->js.x, nc->js.y); UE_LOG(LogWiimote, Log, TEXT("nunchuk joystick calibration (min, center, max): x: %i, %i, %i y: %i, %i, %i"), nc->js.min.x, nc->js.center.x, nc->js.max.x, nc->js.min.y, nc->js.center.y, nc->js.max.y); } /* * If IR tracking is enabled then print the coordinates * on the virtual screen that the wiimote is pointing to. * * Also make sure that we see at least 1 dot. */ if (WIIUSE_USING_IR(wm)) { /* go through each of the 4 possible IR sources */ for (int i = 0; i < 4; ++i) { /* check if the source is visible */ if (wm->ir.dot[i].visible) { UE_LOG(LogWiimote, Log, TEXT("IR source %i: (%u, %u)"), i, wm->ir.dot[i].x, wm->ir.dot[i].y); } } UE_LOG(LogWiimote, Log, TEXT("IR cursor: (%u, %u)"), wm->ir.x, wm->ir.y); UE_LOG(LogWiimote, Log, TEXT("IR z distance: %f"), wm->ir.z); } // Update motion controls. FVector Tilt(0, 0, 0); FVector RotationRate(0, 0, 0); FVector Gravity(0, 0, 0); FVector Acceleration(0, 0, 0); /* if the accelerometer is turned on then print angles */ if (WIIUSE_USING_ACC(wm)) { Tilt.X = -wm->orient.pitch; Tilt.Y = wm->orient.yaw; Tilt.Z = wm->orient.roll; Acceleration.X = wm->accel.x; Acceleration.Y = wm->accel.y; Acceleration.Z = wm->accel.z; } if (wm->exp.type == EXP_MOTION_PLUS || wm->exp.type == EXP_MOTION_PLUS_NUNCHUK) { RotationRate.X = -wm->exp.mp.angle_rate_gyro.pitch; RotationRate.Y = wm->exp.mp.angle_rate_gyro.yaw; RotationRate.Z = wm->exp.mp.angle_rate_gyro.roll; } Gravity.X = wm->gforce.x; Gravity.Y = wm->gforce.y; Gravity.Z = wm->gforce.z; MessageHandler->OnMotionDetected(Tilt, RotationRate, Gravity, Acceleration, id); }
//---------------------------------------------------------------------------- void CpuMassSpringVolume::Update(float time) { // Runge-Kutta fourth-order solver. float halfTime = time + mHalfStep; float fullTime = time + mStep; // Compute the first step. int c, r, s, i; for (s = 0, i = 0; s < mNumSlices; ++s) { for (r = 0; r < mNumRows; ++r) { for (c = 0; c < mNumColumns; ++c, ++i) { if (mInvMass[i] > 0.0f) { mPAllTmp[i].d1 = mVelocity[i]; mVAllTmp[i].d1 = Acceleration(i, c, r, s, time, mPosition, mVelocity); } } } } for (s = 0, i = 0; s < mNumSlices; ++s) { for (r = 0; r < mNumRows; ++r) { for (c = 0; c < mNumColumns; ++c, ++i) { if (mInvMass[i] > 0.0f) { mPTmp[i] = mPosition[i] + mHalfStep*mPAllTmp[i].d1; mVTmp[i] = mVelocity[i] + mHalfStep*mVAllTmp[i].d1; } else { mPTmp[i] = mPosition[i]; mVTmp[i] = Vector3<float>::Zero(); } } } } // Compute the second step. for (s = 0, i = 0; s < mNumSlices; ++s) { for (r = 0; r < mNumRows; ++r) { for (c = 0; c < mNumColumns; ++c, ++i) { if (mInvMass[i] > 0.0f) { mPAllTmp[i].d2 = mVelocity[i]; mVAllTmp[i].d2 = Acceleration(i, c, r, s, halfTime, mPTmp, mVTmp); } } } } for (s = 0, i = 0; s < mNumSlices; ++s) { for (r = 0; r < mNumRows; ++r) { for (c = 0; c < mNumColumns; ++c, ++i) { if (mInvMass[i] > 0.0f) { mPTmp[i] = mPosition[i] + mHalfStep*mPAllTmp[i].d2; mVTmp[i] = mVelocity[i] + mHalfStep*mVAllTmp[i].d2; } else { mPTmp[i] = mPosition[i]; mVTmp[i] = Vector3<float>::Zero(); } } } } // Compute the third step. for (s = 0, i = 0; s < mNumSlices; ++s) { for (r = 0; r < mNumRows; ++r) { for (c = 0; c < mNumColumns; ++c, ++i) { if (mInvMass[i] > 0.0f) { mPAllTmp[i].d3 = mVelocity[i]; mVAllTmp[i].d3 = Acceleration(i, c, r, s, halfTime, mPTmp, mVTmp); } } } } for (s = 0, i = 0; s < mNumSlices; ++s) { for (r = 0; r < mNumRows; ++r) { for (c = 0; c < mNumColumns; ++c, ++i) { if (mInvMass[i] > 0.0f) { mPTmp[i] = mPosition[i] + mStep*mPAllTmp[i].d3; mVTmp[i] = mVelocity[i] + mStep*mVAllTmp[i].d3; } else { mPTmp[i] = mPosition[i]; mVTmp[i] = Vector3<float>::Zero(); } } } } // Compute the fourth step. for (s = 0, i = 0; s < mNumSlices; ++s) { for (r = 0; r < mNumRows; ++r) { for (c = 0; c < mNumColumns; ++c, ++i) { if (mInvMass[i] > 0.0f) { mPAllTmp[i].d4 = mVelocity[i]; mVAllTmp[i].d4 = Acceleration(i, c, r, s, fullTime, mPTmp, mVTmp); } } } } for (s = 0, i = 0; s < mNumSlices; ++s) { for (r = 0; r < mNumRows; ++r) { for (c = 0; c < mNumColumns; ++c, ++i) { if (mInvMass[i] > 0.0f) { mPosition[i] += mSixthStep*(mPAllTmp[i].d1 + 2.0f*(mPAllTmp[i].d2 + mPAllTmp[i].d3) + mPAllTmp[i].d4); mVelocity[i] += mSixthStep*(mVAllTmp[i].d1 + 2.0f*(mVAllTmp[i].d2 + mVAllTmp[i].d3) + mVAllTmp[i].d4); } } } } }
VehicleState(Position pos = Position(), Speed s = Speed(), Heading h = Heading(), Acceleration a = Acceleration(), VehicleSize vsize = VehicleSize()) : position(pos), speed(s), heading(h), acceleration(a), size(vsize) {}
/** Sets the acceleration in m/s^2 */ static Acceleration InMetersPerSecondSquared(double a) { return Acceleration((int16_t)(a/0.01f)); }
void RigidBodySimulator::Simulate(){ // //check item number if (pObjectList.size() == 0 || pForceList.size() == 0){ //not enough items //cannot preceed std::cerr << "not enough object/force!" << std::endl; } else{ // able to simulate //traverse objects for (auto item : pObjectList){ //traverse forces isCollide = false; //collision detection for (auto other_item : pObjectList){ if (other_item == item) //same item continue; else{ //check coll8ison if (CheckObjectCollision(item, other_item)){ //handle collision HandleCollision(item, other_item); isCollide = true; } } }//end travere other objects //sumarize all forces //if (item->getALLForces().size() == 0){ //check if no force apply // continue; //} std::vector<Physical_Force*>forces = item->getALLForces(); Physical_Force totalForce = *forces[0]; if (forces.size() > 1){ for (int i = 0; i < forces.size(); i++){ if (i = 0){ continue; } totalForce += *forces[i]; } } //if (haveGravity){ //gravity applied // totalForce += Physical_Force(Vec3f(0.0, 0.0, -9.8)*item->GetMass()); //for (auto force : item->getALLForces()){ // // // totalForce += *force; //} //update accleration /////////////////////// if (true/*!isCollide*/){ //applyforce only when no collision Acceleration acclerate = Acceleration(&totalForce, item->GetMass()); //update velocity //////////////////////////// Vec3f inspedct = (acclerate*dt).getunifiedVector(); Velocity vincre = acclerate*dt; //Velocity vincre = Velocity(inspedct); //Velocity i = item->GetVelovity() + vincre; item->SetVelocity(item->GetVelovity() + vincre); } //update transition ////////////////////////////////// Vec3f dis = item->GetVelovity().getunifiedVector()*dt; //item->Translate(item->GetVelovity()*dt); item->Translate(dis); item->Rotation(0.5); } } }