- #include "Bike.h"
- #include "ResourceManager.h"
- #include <algorithm>
- #include <cmath>
- #include "AudioManager.h"
- #include "Trail.h"
- #include "DynamicTrail.h"
- #include "math.h"
- Bike::Bike(b2World* world, float x, float y, float width, float height) :
- GameplayState(width, height),
- m_motorSpeed(0.0f),
- m_maxMotorSpeed(500.0f),
- m_maxSpeed(0.05),
- m_acceleration(5.0f),
- m_braking(200.0f),
- m_isAccelerating(false),
- m_isBraking(false) {
- b2BodyDef carBodyDef;
- carBodyDef.type = b2_dynamicBody;
- carBodyDef.position.Set(x, y + 50);
- m_carBody = world->CreateBody(&carBodyDef);
- // Define the car shape and fixture
- b2PolygonShape carBox;
- carBox.SetAsBox(2.0f, 1.0f);
- b2FixtureDef carFixtureDef;
- carFixtureDef.shape = &carBox;
- carFixtureDef.density = 10.0f;
- carFixtureDef.friction = 0.3f;
- m_carBody->CreateFixture(&carFixtureDef);
- // Define the wheel bodies and fixtures
- b2BodyDef wheelBodyDef;
- wheelBodyDef.type = b2_dynamicBody;
- b2CircleShape wheelShape;
- wheelShape.m_radius = 0.5f;
- b2FixtureDef wheelFixtureDef;
- wheelFixtureDef.shape = &wheelShape;
- wheelFixtureDef.density = 1.5f;
- wheelFixtureDef.friction = 0.3f;
- // Create the left wheel
- wheelBodyDef.position.Set(x - 1.5f, y - 2.0f);
- m_leftWheel = world->CreateBody(&wheelBodyDef);
- m_leftWheel->CreateFixture(&wheelFixtureDef);
- // Create the right wheel
- wheelBodyDef.position.Set(x + 1.5f, y - 2.0f);
- m_rightWheel = world->CreateBody(&wheelBodyDef);
- m_rightWheel->CreateFixture(&wheelFixtureDef);
- // Define the joints for the wheels
- b2RevoluteJointDef leftJointDef;
- leftJointDef.bodyA = m_carBody;
- leftJointDef.bodyB = m_leftWheel;
- leftJointDef.localAnchorA.Set(-1.5f, -1.0f);
- leftJointDef.localAnchorB.Set(0.0f, 0.0f);
- leftJointDef.enableMotor = true;
- leftJointDef.maxMotorTorque = 10.0f;
- leftJointDef.motorSpeed = 0.01f;
- m_leftJoint = static_cast<b2RevoluteJoint*>(world->CreateJoint(&leftJointDef));
- b2RevoluteJointDef rightJointDef;
- rightJointDef.bodyA = m_carBody;
- rightJointDef.bodyB = m_rightWheel;
- rightJointDef.localAnchorA.Set(1.5f, -1.0f);
- rightJointDef.localAnchorB.Set(0.0f, 0.0f);
- rightJointDef.enableMotor = true;
- rightJointDef.maxMotorTorque = 10.0f;
- rightJointDef.motorSpeed = 0.01f;
- m_rightJoint = static_cast<b2RevoluteJoint*>(world->CreateJoint(&rightJointDef));
- std::cout<<"Body: " << m_carBody->GetPosition().x << " " << m_carBody->GetPosition().y << std::endl;
- std::cout<<"LWheel: " << m_leftWheel->GetPosition().x << " " << m_leftWheel->GetPosition().y << std::endl;
- std::cout<<"Rwheel: " << m_rightWheel->GetPosition().x << " " << m_rightWheel->GetPosition().y << std::endl;
- }
- void Bike::update(float deltaTime, sf::RenderWindow &window) {
- b2Vec2 velocity = m_carBody->GetLinearVelocity();
- float speed = velocity.Length();
- // Apply motor forces only if wheels are touching the ground
- if (m_leftWheel->GetContactList() || m_rightWheel->GetContactList()) {
- if (m_isAccelerating && speed < m_maxSpeed) {
- m_motorSpeed = std::min(m_motorSpeed + m_acceleration * deltaTime, m_maxMotorSpeed);
- }
- else if (m_isBraking) {
- m_motorSpeed = std::max(m_motorSpeed - m_braking * deltaTime, -m_maxMotorSpeed);
- }
- else {
- // Apply friction when no input
- m_motorSpeed *= 0.99f;
- }
- if (speed >= m_maxSpeed) {
- m_motorSpeed = std::min(m_motorSpeed, m_maxSpeed / (m_leftWheel->GetFixtureList()->GetShape()->m_radius * m_leftJoint->GetJointSpeed()));
- }
- m_leftJoint->SetMotorSpeed(m_motorSpeed);
- m_rightJoint->SetMotorSpeed(m_motorSpeed);
- }
- else {
- // In air, wheels shouldn't affect movement
- m_leftJoint->SetMotorSpeed(0);
- m_rightJoint->SetMotorSpeed(0);
- }
- m_world->Step(deltaTime, 8, 3);
- }
- Bike::Bike(float& width, float& height, b2World& world, std::unique_ptr<Trail>& trail)
- : GameplayStateMode(width, height, world, trail),
- m_playerX(0.0f), m_speedFactor(4.0f),
- m_velocity(0.0f, 0.0f), m_maxSpeed(5.0f),
- m_acceleration(0.55f), m_deceleration(0.13f) {
- // Create dynamic body (bike)
- b2BodyDef dynamicBodyDef;
- dynamicBodyDef.type = b2_dynamicBody;
- dynamicBodyDef.position.Set(m_trail->getSFVertices()[0].x / SCALE, m_trail->getSFVertices()[0].y / SCALE);
- m_dynamicBody = m_world.CreateBody(&dynamicBodyDef);
- b2CircleShape dynamicCircle;
- dynamicCircle.m_radius = 1.0f;
- b2FixtureDef fixtureDef;
- fixtureDef.shape = &dynamicCircle;
- fixtureDef.density = 1.0f;
- fixtureDef.friction = 0.3f;
- m_dynamicBody->CreateFixture(&fixtureDef);
- m_motorSprite.setTexture(ResourceManager::getInstance().getTexture("motor"));
- m_motorSprite.setScale(0.5f, 0.5f);
- sf::FloatRect spriteBounds = m_motorSprite.getLocalBounds();
- m_motorSprite.setOrigin(spriteBounds.width / 2, spriteBounds.height);
- sf::Vector2f initialPos = B2ToSF(m_dynamicBody->GetPosition(), SCALE);
- m_motorSprite.setPosition(initialPos);
- m_trail->updateTrailView(initialPos);
- }
- // void Bike::handleInput(const sf::Event& event) {
- // if (event.type == sf::Event::KeyPressed || event.type == sf::Event::KeyReleased) {
- // bool rightPressed = sf::Keyboard::isKeyPressed(sf::Keyboard::Right);
- // bool leftPressed = sf::Keyboard::isKeyPressed(sf::Keyboard::Left);
- // if (event.type == sf::Event::KeyPressed) {
- // if (rightPressed && !leftPressed) {
- // accelerate();
- // }
- // else if (leftPressed && !rightPressed) {
- // brake();
- // }
- // }
- // if (event.type == sf::Event::KeyReleased) {
- // if (event.key.code == sf::Keyboard::Right) {
- // AudioManager::getInstance().stopSound("motorChangeGear");
- // }
- // if (!rightPressed && !leftPressed) {
- // // Decelerate when no key is pressed or both keys are pressed
- // if (m_velocity.x > 0) {
- // m_velocity.x = std::max(0.0f, m_velocity.x - m_deceleration);
- // }
- // else if (m_velocity.x < 0) {
- // m_velocity.x = std::min(0.0f, m_velocity.x + m_deceleration);
- // }
- // }
- // }
- // // Clamp velocity to max speed
- // //m_velocity.x = std::clamp(m_velocity.x, -m_maxSpeed, m_maxSpeed);
- // if (m_velocity.x > m_maxSpeed)
- // {
- // m_velocity.x = m_maxSpeed;
- // }
- // }
- // std::cout << m_motorSprite.getPosition().x << " " << m_motorSprite.getPosition().y << std::endl;
- // std::cout << "dynamic body: " << m_dynamicBody->GetPosition().x << " " << m_dynamicBody->GetPosition().y << std::endl;
- // }
- // >>>>>>> master
- void Bike::render(sf::RenderWindow& window) {
- window.draw(m_trailShape);
- sf::RectangleShape carShape = createRectangleShape(m_carBody, 4.0f, 2.0f);
- carShape.setFillColor(sf::Color::Blue);
- window.draw(carShape);
- sf::CircleShape leftWheelShape = createCircleShape(m_leftWheel, 0.5f);
- leftWheelShape.setFillColor(sf::Color::Red);
- window.draw(leftWheelShape);
- sf::CircleShape rightWheelShape = createCircleShape(m_rightWheel, 0.5f);
- rightWheelShape.setFillColor(sf::Color::Red);
- window.draw(rightWheelShape);
- }
- void Bike::handleInput(sf::Event event) {
- if (event.type == sf::Event::KeyPressed) {
- if (event.key.code == sf::Keyboard::Right) {
- m_carBody->ApplyLinearImpulseToCenter({ 10,0 }, true);
- m_isAccelerating = true;
- m_isBraking = false;
- }
- else if (event.key.code == sf::Keyboard::Left) {
- m_carBody->ApplyLinearImpulseToCenter({-10,0}, true);
- m_isAccelerating = false;
- m_isBraking = true;
- }
- //else if (event.key.code == sf::Keyboard::Left && event.key.code == sf::Keyboard::Up) {
- // m_carBody->ApplyLinearImpulseToCenter({ -50,0 }, true);
- // m_isAccelerating = false;
- // m_isBraking = true;
- //}
- //else if (event.key.code == sf::Keyboard::Left && event.key.code == sf::Keyboard::Down) {
- // m_carBody->ApplyLinearImpulseToCenter({ -50,0 }, true);
- // m_isAccelerating = false;
- // m_isBraking = true;
- //}
- //else if (event.key.code == sf::Keyboard::Right && event.key.code == sf::Keyboard::Up) {
- // m_carBody->ApplyLinearImpulseToCenter({ 50,0 }, true);
- // m_carBody->ApplyAngularImpulse(50, true);
- // m_isAccelerating = false;
- // m_isBraking = true;
- //}
- //else if (event.key.code == sf::Keyboard::Right && event.key.code == sf::Keyboard::Down) {
- // m_carBody->ApplyLinearImpulseToCenter({ 50,0 }, true);
- // m_isAccelerating = false;
- // m_isBraking = true;
- //}
- =======
- // DynamicTrail* dynamicTrail = dynamic_cast<DynamicTrail*>(m_trail.get());
- // if (dynamicTrail) {
- // // Access DynamicTrail specific methods
- // sf::Vector2f sfPosition = B2ToSF(position, SCALE);
- // sf::Vector2f closestPoint = dynamicTrail->findClosestPointOnTrail(sfPosition);
- // // Set the bike's position to the closest point on the trail
- // m_dynamicBody->SetTransform(SFToB2(closestPoint, SCALE), m_dynamicBody->GetAngle());
- // // Update sprite position
- // m_motorSprite.setPosition(closestPoint);
- // // Calculate angle based on trail slope
- // float angle = dynamicTrail->calculateTrailAngle(closestPoint);
- // m_motorSprite.setRotation(angle);
- // dynamicTrail->updateTrailView(closestPoint);
- // // Check if the player has reached the last vertex
- // auto playerPosition = getPosition();
- // if (playerPosition.x >= dynamicTrail->getSFVertices().back().x) {
- // sf::Vector2f startPoint = dynamicTrail->getSFVertices()[0];
- // m_motorSprite.setPosition(startPoint.x, startPoint.y);
- // m_dynamicBody->SetTransform(SFToB2(startPoint, SCALE), m_dynamicBody->GetAngle());
- // dynamicTrail->updateTrailView(startPoint);
- // }
- // }
- // // Step the Box2D world
- // m_world.Step(deltaTime, 8, 3);
- // }
- // void Bike::render(sf::RenderWindow& window) {
- // window.setView(m_trail->getTrailView());
- // // Fill enclosed areas
- // //m_trail->fillEnclosedAreas(window);
- // // Draw the trail line
- // window.draw(m_trail->getTrailShape());
- // >>>>>>> master
- // }
- else if (event.type == sf::Event::KeyReleased) {
- if (event.key.code == sf::Keyboard::Right || event.key.code == sf::Keyboard::Left) {
- m_isAccelerating = false;
- m_isBraking = false;
- }
- }
- }
- sf::RectangleShape Bike::createRectangleShape(b2Body* body, float width, float height) {
- sf::RectangleShape shape(sf::Vector2f(width * SCALE, height * SCALE));
- shape.setOrigin(width * SCALE / 2.0f, height * SCALE + 22.5f);
- shape.setPosition(body->GetPosition().x * SCALE, (body->GetPosition().y * SCALE));
- shape.setRotation(body->GetAngle() * 180.0f / b2_pi);
- sf::Texture texture;
- texture.loadFromFile("Bike.png");
- shape.setTexture(&texture);
- return shape;
- }
- sf::CircleShape Bike::createCircleShape(b2Body* body, float radius) {
- sf::CircleShape shape(radius * SCALE);
- shape.setOrigin(radius * SCALE, radius * SCALE);
- // Adjust the position to place the wheels below the car body
- // Assuming SCALE is a factor to convert Box2D world units to SFML pixels
- float xPos = body->GetPosition().x * SCALE;
- float yPos = body->GetPosition().y * SCALE;// Adjust for the wheel's radius
- shape.setPosition(xPos, yPos);
- return shape;
- }
- void Bike::accelerate() {
- // Apply torque to accelerate in the right direction
- //m_dynamicBody->ApplyTorque(-20.0f, true); // Use negative torque for clockwise rotation
- m_velocity.x += m_acceleration;
- m_velocity.x = std::min(m_velocity.x, m_maxSpeed);
- AudioManager::getInstance().playSound("motorChangeGear");
- }
- void Bike::brake() {
- m_velocity.x -= m_deceleration;
- }
- sf::Vector2f Bike::getPosition() const { return m_motorSprite.getPosition(); }
- void Bike::setPosition(const sf::Vector2f& position) { m_motorSprite.setPosition(position); }
- float Bike::getSpeed() const { return std::abs(m_velocity.x); }
[text] aa
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new: