void TRBullet::setDirection(TRDirection ndir){ TRSprite::setDirection(ndir); if(ndir == TRDirectionUp){ setVelY(-vel); setVelX(0); }else if(ndir == TRDirectionLeft){ setVelX(-vel); setVelY(0); }else if(ndir == TRDirectionRight){ setVelX(vel); setVelY(0); }else{ setVelX(0); setVelY(vel); } }
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); }
Ball::Ball(double x, double y, double radius, double speedX, double speedY, sf::Color color, bool isActive) { btCollisionShape* shape = new btSphereShape(radius); btDefaultMotionState* mState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(x, y, 0))); double mass; if(isActive) { mass = calculateMass(radius); } else { mass = 0; } btVector3 inertia(0, 0, 0); shape->calculateLocalInertia(mass, inertia); btRigidBody::btRigidBodyConstructionInfo ci(mass, mState, shape, inertia); rigidBody = new btRigidBody(ci); rigidBody->setActivationState(DISABLE_DEACTIVATION); rigidBody->setRestitution(defaultRestitution); rigidBody->setDamping(0, 0); rigidBody->setFriction(defaultFriction); setVelX(speedX); setVelY(speedY); this->radius = radius; this->color = color; this->isActive = isActive; objectType = OBJECT_TYPE_BALL; }
void PhysicsComponent::correctPosition(GameObject& other, Scene& scene, Axis axis) { ColliderComponent* otherCollider = other.getComponent<ColliderComponent>(); switch(axis) { case Axis::X: if (isMovingLeft()) { owner_.setPosX(otherCollider->getRight() - collider_->getOffsetX()); } else { owner_.setPosX(otherCollider->getLeft() - collider_->getWidth() - collider_->getOffsetX()); } setVelX(0.0f); break; case Axis::Y: if (isMovingUp()) { owner_.setPosY(otherCollider->getBottom() - collider_->getOffsetY()); } else { owner_.setPosY(otherCollider->getTop() - collider_->getHeight() - collider_->getOffsetY()); falling_ = false; } setVelY(0.0f); break; default: __debugbreak(); break; } }
Entity Block::getBlock() { // set position auto pos = new CPosition; pos->setX(32); pos->setY(32); E.addComponent(std::type_index(typeid(CPosition)), pos); // set sprite from a spritesheet auto spr = new CSprite; spr->setSize(32); spr->setSprite(this->getSprite(0, 0)); // coordinates in spritesheet E.addComponent(std::type_index(typeid(CSprite)), spr); // set state (0 = moveable) auto sta = new CState; sta->setS(0); E.addComponent(std::type_index(typeid(CState)), sta); // set velocity auto vel = new CVelocity; vel->setVelX(32); vel->setVelY(32); E.addComponent(std::type_index(typeid(CVelocity)), vel); return E; }
void swarm::chaseBuggy(boundingBox bBox) { boundingBox * tb = new boundingBox(); tb->initBox(b->getX() - 250, b->getY() - 250, b->getWidth() + 350, b->getHeight() + 350); if (tb->checkIntersect(bBox)) { intersect = true; if (b->getLeft() < bBox.getLeft() && b->getRight() > bBox.getRight() && b->getY() < bBox.getY()) { setVelY(2); } if (b->getLeft() < bBox.getLeft() && b->getRight() > bBox.getRight() && b->getY() > bBox.getY()) { setVelY(-2); } if (b->getTop() < bBox.getTop() && b->getBottom() > bBox.getBottom() && b->getX() > bBox.getX()) { setVelX(-2); } if (b->getTop() < bBox.getTop() && b->getBottom() > bBox.getBottom() && b->getRight() < bBox.getX()) { setVelX(2); } } else { intersect = false; } if (!intersect) { if (getPosY() > VIEWPORT_UP + 40) { setVelY(-2); } else { setVelX(0); } } }
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 Hero::moveDown() { if(y >= (mTotalHeight / 8) + sHEIGHT) { } else { animationRow = 3; setVelY(5); dirY = 1; } }
void Hero::moveUp() { if(y <= 0 + 15 + (mTotalHeight / 8)) { } else { animationRow = 0; setVelY(5); dirY = -1; } }
void TREnemy::moveRandom(){ if(randrem >= 30){ int curdir = rand()%4; setDirection(curdir); if(curdir == TRDirectionUp){ setVelY(-vel); setVelX(0); }else if(curdir == TRDirectionLeft){ setVelX(-vel); setVelY(0); }else if(curdir == TRDirectionRight){ setVelX(vel); setVelY(0); }else{ setVelX(0); setVelY(vel); } randrem = 0; return; } randrem++; TRSprite::move(); }
void MobileEntity::placeAtEdge(const GameEntity& g) { ///@todo implement // collision detection for walls - so that the player can "land" on them and run into them // perhaps abstract it to the Mobileentity level because other mobile entities will need hit detection for walls as well CollisionBox b1=getHitBox(); CollisionBox b2=g.getHitBox(); //Tells whether or not the midpoint is at all within the mid point of the other box vec3 d1=b1.getTranslate()-getVel()-b1.getDimensions()/3-b2.getPoint1(); vec3 d2=b1.getTranslate()-getVel()+b1.getDimensions()/3-b2.getPoint2(); vec3 midDist=b1.getTranslate()-b2.getTranslate(); bool midInX=(d1.x>0)!=(d2.x>0); bool midInY=(d1.y>0)!=(d2.y>0); bool midInZ=(d1.z>0)!=(d2.z>0); if(!midInX&&!midInY&&getVel().z==0|| !midInX&&!midInZ&&getVel().y==0|| !midInZ&&!midInY&&getVel().x==0 ) return; vec3 newPos; //within the "x prism" float dir=0; float EASE_VAL=.05; float EASE_SCALE_VAL=3.0; if(midInY&&midInZ||!midInX){ if(midDist.x!=0)dir=midDist.x/abs(midDist.x); else dir=0; if(abs(d1.x)>b1.getDimensions().x/EASE_SCALE_VAL||abs(d2.x)>b1.getDimensions().x/EASE_SCALE_VAL)dir=0; if(abs(d1.x)<abs(d2.x)){ if(getVel().x<0) { newPos.x=b2.getPoint1().x+b1.getDimensions().x/2; } }else{ if(getVel().x>0) { newPos.x=b2.getPoint2().x-b1.getDimensions().x/2; } } if(newPos.x!=0){ setTranslateX(newPos.x); setVelX(0); } translate(EASE_VAL*dir,0,0); } //within the "y prism" if(midInX&&midInZ||!midInY){ if(midDist.y!=0)dir=midDist.y/abs(midDist.y); else dir=0; if(abs(d1.y)>b1.getDimensions().y/EASE_SCALE_VAL||abs(d2.y)>b1.getDimensions().y/EASE_SCALE_VAL)dir=0; if(abs(d1.y)<abs(d2.y)){ if(getVel().y<0) { newPos.y=b2.getPoint1().y+b1.getDimensions().y/2; _jumpCount=1; } }else{ if(getVel().y>0) { newPos.y=b2.getPoint2().y-b1.getDimensions().y/2; } } if(newPos.y!=0){ setTranslateY(newPos.y); setVelY(0); } setVel(getVel()*.85); if(dot(getVel(),getVel())<.01){ setVel(0,0,0); } translate(0,EASE_VAL*dir,0); } //within the "z prism" if(midInX&&midInY||!midInZ){ if(midDist.y!=0)dir=midDist.y/abs(midDist.y); else dir=0; if(abs(d1.z)>b1.getDimensions().z/EASE_SCALE_VAL||abs(d2.z)>b1.getDimensions().z/EASE_SCALE_VAL)dir=0; if(abs(d1.z)<abs(d2.z)){ if(getVel().z<0) { newPos.z=b2.getPoint1().z+b1.getDimensions().z/2; } }else{ if(getVel().z>0) { newPos.z=b2.getPoint2().z-b1.getDimensions().z/2; } } if(newPos.z!=0){ setTranslateZ(newPos.z); setVelZ(0); } translate(0,0,EASE_VAL*dir); } vec3 v=getTranslate()-newPos; }