void ode_evaluate( CppAD::vector<Float> &x , size_t m , CppAD::vector<Float> &fm ) { typedef CppAD::vector<Float> Vector; size_t n = x.size(); size_t ell; CPPAD_ASSERT_KNOWN( m == 0 || m == 1, "ode_evaluate: m is not zero or one" ); CPPAD_ASSERT_KNOWN( ((m==0) & (fm.size()==n)) || ((m==1) & (fm.size()==n*n)), "ode_evaluate: the size of fm is not correct" ); if( m == 0 ) ell = n; else ell = n + n * n; // set up the case we are integrating Float ti = 0.; Float tf = 1.; Float smin = 1e-5; Float smax = 1.; Float scur = 1.; Float erel = 0.; vector<Float> yi(ell), eabs(ell); size_t i, j; for(i = 0; i < ell; i++) { eabs[i] = 1e-10; if( i < n ) yi[i] = 1.; else yi[i] = 0.; } // return values Vector yf(ell), ef(ell), maxabs(ell); size_t nstep; // construct ode method for taking one step ode_evaluate_method<Float> method(m, x); // solve differential equation yf = OdeErrControl(method, ti, tf, yi, smin, smax, scur, eabs, erel, ef, maxabs, nstep); if( m == 0 ) { for(i = 0; i < n; i++) fm[i] = yf[i]; } else { for(i = 0; i < n; i++) for(j = 0; j < n; j++) fm[i * n + j] = yf[n + i * n + j]; } return; }
void reverse() { if (xf() == 0) { xf() = 1; xi()--; } else if (xf() == 1) { xf() = 0; xi()++; } else if (yf() == 0) { yf() = 1; yi()--; } else if (yf() == 1) { yf() = 0; yi()++; } }
void calcy(FILE *fl1,FILE *fl2){ int i,k; for(i=0;i<N*range;i++){ y[i]=yf(i); fprintf(fl1,"%d %e\n",i,y[i]);} for (i=0;i<N*range;i++){ y[i]=0; for (k=0;k<=i;k++) y[i]+=x[i-k]*K[k]; fprintf(fl2,"%d %e\n",i,y[i]);}}
void TensorMechanicsPlasticTensileMulti::activeConstraints(const std::vector<Real> & f, const RankTwoTensor & stress, const Real & intnl, const RankFourTensor & Eijkl, std::vector<bool> & act, RankTwoTensor & returned_stress) const { act.assign(3, false); if (f[0] <= _f_tol && f[1] <= _f_tol && f[2] <= _f_tol) { returned_stress = stress; return; } Real returned_intnl; std::vector<Real> dpm(3); RankTwoTensor delta_dp; std::vector<Real> yf(3); bool trial_stress_inadmissible; doReturnMap(stress, intnl, Eijkl, 0.0, returned_stress, returned_intnl, dpm, delta_dp, yf, trial_stress_inadmissible); for (unsigned i = 0 ; i < 3 ; ++i) act[i] = (dpm[i] > 0); }
void step(const vb::Array<double> &o) { int r = 0; double oo = o.at(vb::ucoo(vb::coo{xi(), yi()})); if (yf() == 1) { r = 1; oo = -1 / oo; yf() = xf(); xf() = 0; } else if (xf() == 1) { r = 2; yf() = 1 - yf(); xf() = 0; } else if (yf() == 0) { r = 3; oo = -1 / oo; yf() = 1 - xf(); xf() = 0; } int di = 0, dj = 0; if (oo < -yf()) { xf() = -yf() / oo; yf() = 0; dj = -1; } else if (oo < 1 - yf()) { xf() = 1; yf() = yf() + oo; di = 1; } else { xf() = (1 - yf()) / oo; yf() = 1; dj = 1; } if (r == 1) { int ti = di; di = dj; dj = -ti; double tx = xf(); xf() = yf(); yf() = 1 - tx; } else if (r == 2) { di = -di; dj = -dj; xf() = 1 - xf(); yf() = 1 - yf(); } else if (r == 3) { int ti = di; di = -dj; dj = ti; double tx = xf(); xf() = 1 - yf(); yf() = tx; } xf() -= di; xi() += di; yf() -= dj; yi() += dj; }
void IO_MovingObject::collision_detection_map() { xf(fx + velx); flipsidesifneeded(); fPrecalculatedY = fy + vely; //Fixes weird float rounding error. Must be computed here before casting to int. Otherwise, this will miss the bottom collision, but then hit the side collision and the player can slide out of 1x1 spaces. float fPlatformVelX = 0.0f; float fPlatformVelY = 0.0f; float fTempY = fy; if(platform) { if(!onice) { fPlatformVelX = platform->fVelX; xf(fx + fPlatformVelX); flipsidesifneeded(); } fPlatformVelY = platform->fVelY; if(platform->fOldVelY < 0.0f) fy += platform->fOldVelY; fPrecalculatedY += platform->fOldVelY; } iHorizontalPlatformCollision = -1; iVerticalPlatformCollision = -1; g_map.movingPlatformCollision(this); fy = fTempY; if(fPrecalculatedY + collisionHeight < 0.0f) { // on top outside of the screen yf(fPrecalculatedY); vely = CapFallingVelocity(GRAVITATION + vely); if(!platform) { inair = true; onice = false; } return; } else if(fPrecalculatedY + collisionHeight >= 480.0f) { //on ground outside of the screen? yi(-collisionHeight); fOldY = fy - 1.0f; onice = false; return; } //Could be optimized with bit shift >> 5 short ty = (short)fy / TILESIZE; short ty2 = ((short)fy + collisionHeight) / TILESIZE; short tx = -1; //----------------------------------------------------------------- // x axis first (--) //----------------------------------------------------------------- if(fy + collisionHeight >= 0.0f) { if(velx + fPlatformVelX > 0.01f || iHorizontalPlatformCollision == 3) { //moving right if(fx + collisionWidth >= 640.0f) { tx = (short)(fx + collisionWidth - 640.0f) / TILESIZE; fOldX -= 640.0f; } else tx = ((short)fx + collisionWidth) / TILESIZE; IO_Block * topblock = g_map.block(tx, ty); IO_Block * bottomblock = g_map.block(tx, ty2); bool fTopBlockSolid = topblock && !topblock->isTransparent() && !topblock->isHidden(); bool fBottomBlockSolid = bottomblock && !bottomblock->isTransparent() && !bottomblock->isHidden(); if(fTopBlockSolid || fBottomBlockSolid) { bool processOtherBlock = true; if(fTopBlockSolid) //collide with top block { if(iHorizontalPlatformCollision == 3) { KillObjectMapHazard(); return; } topblock->collide(this, 1); flipsidesifneeded(); removeifprojectile(this, true, true); processOtherBlock = false; SideBounce(true); } if(processOtherBlock && fBottomBlockSolid) //then bottom { if(iHorizontalPlatformCollision == 3) { KillObjectMapHazard(); return; } bottomblock->collide(this, 1); flipsidesifneeded(); removeifprojectile(this, true, true); SideBounce(true); } } else if((g_map.map(tx, ty) & tile_flag_solid) || (g_map.map(tx, ty2) & tile_flag_solid)) { //collision on the right side. if(iHorizontalPlatformCollision == 3) { KillObjectMapHazard(); return; } xf((float)((tx << 5) - collisionWidth) - 0.2f); //move to the edge of the tile (tile on the right -> mind the object width) fOldX = fx; if(velx > 0.0f) velx = -velx; flipsidesifneeded(); removeifprojectile(this, true, true); SideBounce(true); } } else if(velx + fPlatformVelX < -0.01f || iHorizontalPlatformCollision == 1) { //moving left tx = (short)fx / TILESIZE; //Just in case fx < 0 and wasn't caught by flipsidesifneeded() if(tx < 0) tx = 0; IO_Block * topblock = g_map.block(tx, ty); IO_Block * bottomblock = g_map.block(tx, ty2); bool fTopBlockSolid = topblock && !topblock->isTransparent() && !topblock->isHidden(); bool fBottomBlockSolid = bottomblock && !bottomblock->isTransparent() && !bottomblock->isHidden(); if(fTopBlockSolid || fBottomBlockSolid) { bool processOtherBlock = true; if(fTopBlockSolid) //collide with top block { if(iHorizontalPlatformCollision == 1) { KillObjectMapHazard(); return; } topblock->collide(this, 3); flipsidesifneeded(); removeifprojectile(this, true, true); processOtherBlock = false; SideBounce(false); } if(processOtherBlock && fBottomBlockSolid) //then bottom { if(iHorizontalPlatformCollision == 1) { KillObjectMapHazard(); return; } bottomblock->collide(this, 3); flipsidesifneeded(); removeifprojectile(this, true, true); SideBounce(false); } } else if((g_map.map(tx, ty) & tile_flag_solid) || (g_map.map(tx, ty2) & tile_flag_solid)) { if(iHorizontalPlatformCollision == 1) { KillObjectMapHazard(); return; } xf((float)((tx << 5) + TILESIZE) + 0.2f); //move to the edge of the tile fOldX = fx; if(velx < 0.0f) velx = -velx; flipsidesifneeded(); removeifprojectile(this, true, true); SideBounce(false); } } } short txl = -1, txr = -1; txl = ix / TILESIZE; if(ix + collisionWidth >= 640) txr = (ix + collisionWidth - 640) / TILESIZE; else txr = (ix + collisionWidth) / TILESIZE; //----------------------------------------------------------------- // then y axis (|) //----------------------------------------------------------------- float fMovingUp = vely; if(platform) fMovingUp = vely + fPlatformVelY - bounce; if(fMovingUp < -0.01f) { ty = (short)(fPrecalculatedY) / TILESIZE; IO_Block * leftblock = g_map.block(txl, ty); IO_Block * rightblock = g_map.block(txr, ty); if(leftblock && !leftblock->isTransparent() && !leftblock->isHidden()) //then left { if(iVerticalPlatformCollision == 2) KillObjectMapHazard(); leftblock->collide(this, 0); return; } if(rightblock && !rightblock->isTransparent() && !rightblock->isHidden()) //then right { if(iVerticalPlatformCollision == 2) KillObjectMapHazard(); rightblock->collide(this, 0); return; } if((g_map.map(txl, ty) & tile_flag_solid) || (g_map.map(txr, ty) & tile_flag_solid)) { if(iVerticalPlatformCollision == 2) KillObjectMapHazard(); yf((float)((ty << 5) + TILESIZE) + 0.2f); fOldY = fy - 1.0f; if(vely < 0.0f) vely = -vely; } else { yf(fPrecalculatedY); vely += GRAVITATION; } if(!platform) { inair = true; onice = false; } } else { //moving down / on ground ty = ((short)fPrecalculatedY + collisionHeight) / TILESIZE; IO_Block * leftblock = g_map.block(txl, ty); IO_Block * rightblock = g_map.block(txr, ty); bool fLeftBlockSolid = leftblock && !leftblock->isTransparent() && !leftblock->isHidden(); bool fRightBlockSolid = rightblock && !rightblock->isTransparent() && !rightblock->isHidden(); if(fLeftBlockSolid || fRightBlockSolid) { bool processOtherBlock = true; if(fLeftBlockSolid) //collide with left block { processOtherBlock = leftblock->collide(this, 2); if(!platform) { inair = false; onice = false; } } if(processOtherBlock && fRightBlockSolid) //then right { rightblock->collide(this, 2); if(!platform) { inair = false; onice = false; } } if(iVerticalPlatformCollision == 0) KillObjectMapHazard(); return; } int leftTile = g_map.map(txl, ty); int rightTile = g_map.map(txr, ty); if((leftTile & tile_flag_solid_on_top) || (rightTile & tile_flag_solid_on_top)) { if((fOldY + collisionHeight) / TILESIZE < ty) { vely = BottomBounce(); yf((float)((ty << 5) - collisionHeight) - 0.2f); fOldY = fy - GRAVITATION; if(!platform) { inair = false; onice = false; } platform = NULL; if(iVerticalPlatformCollision == 0) KillObjectMapHazard(); return; } } bool fSuperDeathTileUnderObject = fObjectDiesOnSuperDeathTiles && (((leftTile & tile_flag_super_death_top) && (rightTile & tile_flag_super_death_top)) || ((leftTile & tile_flag_super_death_top) && !(rightTile & tile_flag_solid)) || (!(leftTile & tile_flag_solid) && (rightTile & tile_flag_super_death_top))); if(((leftTile & tile_flag_solid) || (rightTile & tile_flag_solid)) && !fSuperDeathTileUnderObject) { vely = BottomBounce(); yf((float)((ty << 5) - collisionHeight) - 0.2f); fOldY = fy; if(!platform) { inair = false; if((leftTile & tile_flag_ice && ((rightTile & tile_flag_ice) || rightTile == tile_flag_nonsolid || rightTile == tile_flag_gap)) || (rightTile & tile_flag_ice && ((leftTile & tile_flag_ice) || leftTile == tile_flag_nonsolid || leftTile == tile_flag_gap))) onice = true; else onice = false; } platform = NULL; if(iVerticalPlatformCollision == 0) KillObjectMapHazard(); } else if(fSuperDeathTileUnderObject) { KillObjectMapHazard(); return; } else { yf(fPrecalculatedY); vely = CapFallingVelocity(GRAVITATION + vely); if(!platform) inair = true; } } if(!platform && inair) onice = false; }
//------------------------------------------------------------------------------ //! void TQuad::update() { if( _img.isNull() ) return; Gfx::Texture* tex = _img->texture(); int xdim = tex->definedWidth(); int ydim = tex->definedHeight(); float txdim = (float)tex->width(); float tydim = (float)tex->height(); Vec4i ui( _u * (float)xdim ); Vec4f uf( Vec4f( ui ) / txdim ); int du0 = ui(1) - ui(0); int du1 = ui(3) - ui(2); int duc = ui(2) - ui(1); Vec4i vi( _v * (float)ydim ); Vec4f vf( Vec4f( vi ) / tydim ); int dv0 = vi(1) - vi(0); int dv1 = vi(3) - vi(2); int dvc = vi(2) - vi(1); ushort* indices; float off = Core::gfx()->oneToOneOffset(); switch( _type ) { case NORMAL: default: { Vec2f xf( 0.0f, _size.x ); Vec2f yf( 0.0f, _size.y ); xf += off; yf += off; _vdata[0] = Vec4f( xf(0), yf(0), uf(0), vf(0) ); _vdata[1] = Vec4f( xf(1), yf(0), uf(1), vf(0) ); _vdata[2] = Vec4f( xf(0), yf(1), uf(0), vf(1) ); _vdata[3] = Vec4f( xf(1), yf(1), uf(1), vf(1) ); indices = _verIndices; } break; case HORIZONTAL: { int maxdu0 = (du0 * (int)_size.x) / (du0 + du1); int maxdu1 = (int)_size.x - maxdu0; CGM::clampMax( du0, maxdu0 ); CGM::clampMax( du1, maxdu1 ); Vec4f xf( 0.0f, (float)du0, _size.x - float(du1), _size.x ); Vec2f yf( 0.0f, _size.y ); xf += off; yf += off; _vdata[0] = Vec4f( xf(0), yf(0), uf(0), vf(0) ); _vdata[1] = Vec4f( xf(1), yf(0), uf(1), vf(0) ); _vdata[2] = Vec4f( xf(2), yf(0), uf(2), vf(0) ); _vdata[3] = Vec4f( xf(3), yf(0), uf(3), vf(0) ); _vdata[4] = Vec4f( xf(0), yf(1), uf(0), vf(1) ); _vdata[5] = Vec4f( xf(1), yf(1), uf(1), vf(1) ); _vdata[6] = Vec4f( xf(2), yf(1), uf(2), vf(1) ); _vdata[7] = Vec4f( xf(3), yf(1), uf(3), vf(1) ); indices = _horIndices; } break; case VERTICAL: { int maxdv0 = (dv0 * (int)_size.y) / (dv0 + dv1); int maxdv1 = (int)_size.y - maxdv0; CGM::clampMax( dv0, maxdv0 ); CGM::clampMax( dv1, maxdv1 ); Vec2f xf( 0.0f, _size.x ); Vec4f yf( 0.0f, (float)dv0, _size.y - (float)(dv1), _size.y ); xf += off; yf += off; _vdata[0] = Vec4f( xf(0), yf(0), uf(0), vf(0) ); _vdata[1] = Vec4f( xf(1), yf(0), uf(1), vf(0) ); _vdata[2] = Vec4f( xf(0), yf(1), uf(0), vf(1) ); _vdata[3] = Vec4f( xf(1), yf(1), uf(1), vf(1) ); _vdata[4] = Vec4f( xf(0), yf(2), uf(0), vf(2) ); _vdata[5] = Vec4f( xf(1), yf(2), uf(1), vf(2) ); _vdata[6] = Vec4f( xf(0), yf(3), uf(0), vf(3) ); _vdata[7] = Vec4f( xf(1), yf(3), uf(1), vf(3) ); indices = _verIndices; } break; case GRID: case CONTOUR: { int maxdu0 = (du0 * (int)_size.x) / (du0 + du1); int maxdu1 = (int)_size.x - maxdu0; CGM::clampMax( du0, maxdu0 ); CGM::clampMax( du1, maxdu1 ); int maxdv0 = (dv0 * (int)_size.y) / (dv0 + dv1); int maxdv1 = (int)_size.y - maxdv0; CGM::clampMax( dv0, maxdv0 ); CGM::clampMax( dv1, maxdv1 ); Vec4f xf( 0.0f, (float)du0, _size.x-(float)(du1), _size.x ); Vec4f yf( 0.0f, (float)dv0, _size.y-(float)(dv1), _size.y ); xf += off; yf += off; _vdata[0] = Vec4f( xf(0), yf(0), uf(0), vf(0) ); _vdata[1] = Vec4f( xf(1), yf(0), uf(1), vf(0) ); _vdata[2] = Vec4f( xf(2), yf(0), uf(2), vf(0) ); _vdata[3] = Vec4f( xf(3), yf(0), uf(3), vf(0) ); _vdata[4] = Vec4f( xf(0), yf(1), uf(0), vf(1) ); _vdata[5] = Vec4f( xf(1), yf(1), uf(1), vf(1) ); _vdata[6] = Vec4f( xf(2), yf(1), uf(2), vf(1) ); _vdata[7] = Vec4f( xf(3), yf(1), uf(3), vf(1) ); _vdata[8] = Vec4f( xf(0), yf(2), uf(0), vf(2) ); _vdata[9] = Vec4f( xf(1), yf(2), uf(1), vf(2) ); _vdata[10] = Vec4f( xf(2), yf(2), uf(2), vf(2) ); _vdata[11] = Vec4f( xf(3), yf(2), uf(3), vf(2) ); _vdata[12] = Vec4f( xf(0), yf(3), uf(0), vf(3) ); _vdata[13] = Vec4f( xf(1), yf(3), uf(1), vf(3) ); _vdata[14] = Vec4f( xf(2), yf(3), uf(2), vf(3) ); _vdata[15] = Vec4f( xf(3), yf(3), uf(3), vf(3) ); indices = _type == GRID ? _horIndices : _contourIndices; } break; case ADJUST_BORDERS: { du0 = du0 * ((int)_size.x-duc ) / (du0+du1); du1 = (int)_size.x-duc-du0; CGM::clampMin( du0, 0 ); CGM::clampMin( du1, 0 ); dv0 = dv0 * ((int)_size.y-dvc ) / (dv0+dv1); dv1 = (int)_size.y-dvc-dv0; CGM::clampMin( dv0, 0 ); CGM::clampMin( dv1, 0 ); Vec4f xf( 0.0f, (float)du0, _size.x-(float)(du1), _size.x ); Vec4f yf( 0.0f, (float)dv0, _size.y-(float)(dv1), _size.y ); xf += off; yf += off; _vdata[0] = Vec4f( xf(0), yf(0), uf(0), vf(0) ); _vdata[1] = Vec4f( xf(1), yf(0), uf(1), vf(0) ); _vdata[2] = Vec4f( xf(2), yf(0), uf(2), vf(0) ); _vdata[3] = Vec4f( xf(3), yf(0), uf(3), vf(0) ); _vdata[4] = Vec4f( xf(0), yf(1), uf(0), vf(1) ); _vdata[5] = Vec4f( xf(1), yf(1), uf(1), vf(1) ); _vdata[6] = Vec4f( xf(2), yf(1), uf(2), vf(1) ); _vdata[7] = Vec4f( xf(3), yf(1), uf(3), vf(1) ); _vdata[8] = Vec4f( xf(0), yf(2), uf(0), vf(2) ); _vdata[9] = Vec4f( xf(1), yf(2), uf(1), vf(2) ); _vdata[10] = Vec4f( xf(2), yf(2), uf(2), vf(2) ); _vdata[11] = Vec4f( xf(3), yf(2), uf(3), vf(2) ); _vdata[12] = Vec4f( xf(0), yf(3), uf(0), vf(3) ); _vdata[13] = Vec4f( xf(1), yf(3), uf(1), vf(3) ); _vdata[14] = Vec4f( xf(2), yf(3), uf(2), vf(3) ); _vdata[15] = Vec4f( xf(3), yf(3), uf(3), vf(3) ); indices = _horIndices; } break; case CENTERED: { Vec2f xf( (_size.x - (float)(xdim))/2.0f, (_size.x + (float)(xdim))/2.0f ); Vec2f yf( (_size.y - (float)(ydim))/2.0f, (_size.y + (float)(ydim))/2.0f ); xf += off; yf += off; // Vertices _vdata[0] = Vec4f( xf(0), yf(0), uf(0), vf(0) ); _vdata[1] = Vec4f( xf(1), yf(0), uf(1), vf(0) ); _vdata[2] = Vec4f( xf(0), yf(1), uf(0), vf(1) ); _vdata[3] = Vec4f( xf(1), yf(1), uf(1), vf(1) ); indices = _verIndices; } break; } RCP<Gfx::IndexBuffer> indexBuffer; RCP<Gfx::VertexBuffer> vertexBuffer; // Create buffers if not done yet. if( _geom->numBuffers() > 0 ) { indexBuffer = _geom->indexBuffer(); vertexBuffer = _geom->buffers()[0]; } else { indexBuffer = Core::gfx()->createBuffer( Gfx::INDEX_FMT_16, Gfx::BUFFER_FLAGS_NONE, 0, 0 ); vertexBuffer = Core::gfx()->createBuffer( Gfx::BUFFER_FLAGS_NONE, 0, 0 ); vertexBuffer->addAttribute( Gfx::ATTRIB_TYPE_POSITION, Gfx::ATTRIB_FMT_32F_32F, 0 ); vertexBuffer->addAttribute( Gfx::ATTRIB_TYPE_TEXCOORD0, Gfx::ATTRIB_FMT_32F_32F, 8 ); _geom->indexBuffer( indexBuffer ); _geom->addBuffer( vertexBuffer ); } // Fill buffer. Core::gfx()->setData( indexBuffer, (size_t)_indicesSize[_type], indices ); Core::gfx()->setData( vertexBuffer, (size_t)_verticesSize[_type], _vdata.data() ); }