bool Enemy::tick(){ if(Game::enemiesRoute.size()<=_nextPositionIndex || _nextPositionIndex<0 || _distanceToEnd<=_velocity) return true; Vec2d p = Game::enemiesRoute[_nextPositionIndex]; double toMove = _velocity; _distanceToEnd -= toMove; while(toMove>0.0){ double d = _position.distance(p); if(d<=_velocity){ _position = p; _nextPositionIndex += 1; /*if(Game::enemiesRoute.size()<=_nextPositionIndex){ _nextPositionIndex = -1; return true; }*/ toMove -= d; p = Game::enemiesRoute[_nextPositionIndex]; }else{ Vec2d t = p-_position; t.normalize(); t *= _velocity; _position += t; toMove = 0.0; } } return false; }
void Spatial::update() { double timeDelta = mTimeDelta.getElapsedTime().asSeconds(); mSteeringForce.truncate(mMaxForce); Vec2d acceleration = mSteeringForce / mMass; mVelocity += acceleration * timeDelta; mVelocity.truncate(mMaxSpeed); mPos += mVelocity * timeDelta; if (mVelocity.lengthSq() > 0.00000001) { Vec2d velocityNorm = mVelocity; velocityNorm.normalize(); setHeading(velocityNorm); } mTimeDelta.restart(); }
bool Spatial::rotateToHeading(Vec2d pos) { Vec2d toTarget = Vec2d(pos-mPos); toTarget.normalize(); double angle = acos(mHeading.dot(pos)); if (angle < 0.000001) return true; if (angle > mMaxTurnRate) angle = mMaxTurnRate; Mat3d rotationMat; rotationMat.rotate(angle*mHeading.sign(toTarget)); rotationMat.transformVec2d(mHeading); rotationMat.transformVec2d(mVelocity); mSide = mHeading.perp(); return false; }
TestAppSimplexGrid::TestAppSimplexGrid( int& id, int WIDTH_, int HEIGHT_ ) : AppSDL2OGL( id, WIDTH_, HEIGHT_ ) { grid.init( 1.0, 8 ); shape=glGenLists(1); glNewList( shape, GL_COMPILE ); glBegin ( GL_POINTS ); for( int i=0; i<10000; i++ ){ double x = randf( -5,5 ); double y = randf( -5,5 ); double da,db; UHALF ia,ib; //bool s = simplexIndex( x+100, y+100, ia,ib, da, db ); bool s = grid.simplexIndex( x, y, ia,ib, da, db ); //printf( " (%3.3f,%3.3f) (%3.3f,%3.3f) (%i,%i) \n", x, y, da, db, ia, ib ); s = false; if( s ){ glColor3f( da, db, 1-da-db ); }else{ //int h = rand_hash( (ia*359) ^ (ib*353) ); int h = (ia*920419823) ^ (ib*941083981); glColor3f( (h&0x0000FF)/255.0f, (h&0x00FF00)/65535.0f, (h&0xFF0000)/16777216.0f ); } glVertex3f( (float)x,(float)y, 0.0f ); } glEnd(); glColor3f(0.1f,0.1f,0.1f); Draw2D::drawSimplexGrid( 10, (float)grid.step ); Vec2d dirHat; p1.set(0.9,0.6); p2.set(8.2,8.3); dirHat.set_sub( p2, p1 ); dirHat.normalize(); nhits = grid.raster_line( dirHat, p1, p2, hits, boundaries, edges ); glEndList(); }
bool Rocket::tick(){ if(Game::exists(_targetEnemy)){ _targetPosition = _targetEnemy->getPosition(); }else{ _targetEnemy = nullptr; } Vec2d p = _targetPosition; double distance = p.distance(_position); if(distance<=_velocity && distance+_distanceTraveled<_maxDistance){ _position = p; damageEnemies(); return true; }else{ p = p-_position; p.normalize(); if(_velocity+_distanceTraveled<_maxDistance){ p *= _velocity; _distanceTraveled += _velocity; _position += p; }else{ p *= _maxDistance-_distanceTraveled; _distanceTraveled = _maxDistance; _position += p; damageEnemies(); return true; } } if(_hitEnemiesInPath && Game::findEnemiesInRange(_position, 0,10).size()>0){ damageEnemies(); return true; } return false; }
void Monster::think(const double elapsedTime) { Entity::think(elapsedTime); if(!mIsDying && mHealth <= 0) kill(); if(mIsStunned) { if(mPosition.Y > -0.5) { mVelocity.Y -= 0.005; mPosition += mVelocity; if(game->getCurrentMap()->getTile(int(mPosition.X + mVelocity.X), int(mPosition.Z)).type == TYPE_WALL) { mVelocity.X = -mVelocity.X; mVelocity *= MONSTER_DAMPING; } if(game->getCurrentMap()->getTile(int(mPosition.X), int(mPosition.Z + mVelocity.Z)).type == TYPE_WALL) { mVelocity.Z = -mVelocity.Z; mVelocity *= MONSTER_DAMPING; } } else { mIsStunned = false; mPosition.Y = -0.5; } } if(mIsDying) { if(mDeathTimer.getElapsedTime() >= 500) mShouldDelete = true; return; } if(mIsFrozen) { if(mFreezeTimer.getElapsedTime() < 5000) { return; } else setFrozen(false); } if(mIsFleeing) { Vec2d motion = Vec2d(playerPos.X-mPosition.X,playerPos.Z-mPosition.Z); motion.normalize(); motion /= 35; if((playerPos.X - mPosition.X) + (playerPos.Z - mPosition.Z) < 0.3) { int goalStr = game->getCurrentMap()->getTile(int(mPosition.X), int(mPosition.Z)).goodCreatureGoal; playerPos = mPosition; for(int i = -1; i < 2; ++i) for(int j = -1; j < 2; ++j) if(game->getCurrentMap()->getTile(i + int(mPosition.X), j + int(mPosition.Z)).type != TYPE_WALL && goalStr < game->getCurrentMap()->getTile(i + int(mPosition.X), j + int(mPosition.Z)).goodCreatureGoal && game->getCurrentMap()->getTile(i + int(mPosition.X), j + int(mPosition.Z)).goodCreatureGoal < 100) { goalStr = game->getCurrentMap()->getTile(i + int(mPosition.X), j + int(mPosition.Z)).goodCreatureGoal; playerPos.X = mPosition.X + i; playerPos.Z = mPosition.Z + j; } } game->getCurrentMap()->setGoal(int(playerPos.X), int(playerPos.Z), 0); game->getCurrentMap()->setGoal(int(mPosition.X), int(mPosition.Z), 0); //Increase position of monster. mPosition -= Vec3d(motion.X(),0,motion.Y()); } else { Vec2d motion = Vec2d(playerPos.X-mPosition.X,playerPos.Z-mPosition.Z); motion.normalize(); motion /= 50; if((playerPos.X - mPosition.X) + (playerPos.Z - mPosition.Z) < 0.3) { int goalStr = game->getCurrentMap()->getTile(int(mPosition.X), int(mPosition.Z)).goodCreatureGoal; playerPos = mPosition; for(int i = -1; i < 2; ++i) for(int j = -1; j < 2; ++j) if(game->getCurrentMap()->getTile(i + int(mPosition.X), j + int(mPosition.Z)).type != TYPE_WALL && goalStr < game->getCurrentMap()->getTile(i + int(mPosition.X), j + int(mPosition.Z)).goodCreatureGoal && game->getCurrentMap()->getTile(i + int(mPosition.X), j + int(mPosition.Z)).goodCreatureGoal < 100) { goalStr = game->getCurrentMap()->getTile(i + int(mPosition.X), j + int(mPosition.Z)).goodCreatureGoal; playerPos.X = mPosition.X + i; playerPos.Z = mPosition.Z + j; } } game->getCurrentMap()->setGoal(int(playerPos.X), int(playerPos.Z), 0); game->getCurrentMap()->setGoal(int(mPosition.X), int(mPosition.Z), 0); //Increase position of monster. mPosition += Vec3d(motion.X(),0,motion.Y()); } /*if(rand() % 500 == 0) { attack(); } if(rand() % 5000 == 0) { makeMonsterNoise(); }*/ if(mVoice) mVoice->setPosition(mPosition); }
Vec2d ModuleWalls::getNOrtho(Vec2d a, Vec2d b){ Vec2d res = Vec2d(-(b-a)[1], (b-a)[0]); res.normalize(); //vector length = 1; return res; }