void SimObject::calculateSprings(SimObject* anotherObject, double delta, double springMaxDistance, double springDistance, double springDamping, double springForce) { double distance = distanceBetween(this, anotherObject); if(distance == 0) return; if(springMaxDistance > 0 && distance > springMaxDistance) { springConnections.erase(std::remove(springConnections.begin(), springConnections.end(), anotherObject), springConnections.end()); anotherObject->incomingSpringConnectionsCount--; return; } double offset = distance - springDistance; double relativeSpeedX = anotherObject->getVelX() - getVelX(); double relativeSpeedY = anotherObject->getVelY() - getVelY(); //TODO remake damping double relativeSpeed = sqrt(relativeSpeedX*relativeSpeedX + relativeSpeedY*relativeSpeedY); double dampingForce = relativeSpeed * springDamping; double dampingForceX, dampingForceY; if(relativeSpeed != 0) { dampingForceX = relativeSpeedX / relativeSpeed * dampingForce; dampingForceY = relativeSpeedY / relativeSpeed * dampingForce; } else { dampingForceX = 0; dampingForceY = 0; } double force; force = offset * springForce - dampingForce; double forceX = (anotherObject->getX() - getX()) / distance * force + dampingForceX; double forceY = (anotherObject->getY() - getY()) / distance * force + dampingForceY; double velX = getVelX(); double velY = getVelY(); velX += forceX / getMass() * delta; velY += forceY / getMass() * delta; setVelX(velX); setVelY(velY); }
void SimObject::calculateGravity(SimObject* anotherObject, double delta, double gravityRadialForce) { double distance = distanceBetween(this, anotherObject); if(distance == 0) return; double force = gravityRadialForce * getMass() * anotherObject->getMass() / (distance*distance); double forceX = (anotherObject->getX() - getX()) / distance * force; double forceY = (anotherObject->getY() - getY()) / distance * force; double velX = getVelX(); double velY = getVelY(); velX += forceX / getMass() * delta; velY += forceY / getMass() * delta; setVelX(velX); setVelY(velY); }
void CShip::draw(sf::RenderWindow* window, float interpolation) { float iX = getX() + (getVelX() * interpolation); float iY = getY() + (getVelY() * interpolation); // Calculate point ahead of ship to point to float rads = getRotation()/3.14*180; float px = iX-30*std::cos(rads); float py = iY-30*std::sin(rads); sprite.setPosition(iX, iY); // Set rotation at the point sprite.setRotation( -std::atan2(px - iX, py - iY)*180/3.14); window->draw(sprite); }
void SimObject::setVelX(double velX) { rigidBody->setLinearVelocity(btVector3(velX, getVelY(), 0)); }