Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Velocity making weird things #755

Closed
2 tasks done
BiomedicLJZ opened this issue Sep 26, 2023 · 1 comment
Closed
2 tasks done

Velocity making weird things #755

BiomedicLJZ opened this issue Sep 26, 2023 · 1 comment

Comments

@BiomedicLJZ
Copy link

Hi, im trying to simulate a bullet being fired at a 45° degree angle but as soon as my world makes the first step the Velocity bombs from 668 m/s to 84 m/s, this is the code:

#include <Box2D/Box2D.h>
#include <iostream>
#include <cmath>

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

int main() {
    // Define the gravity vector
    b2Vec2 gravity(0.0f, -9.81f);

    // Construct a world object, which will hold and simulate the rigid bodies
    b2World world(gravity);

    // Create a dynamic body for the bullet
    b2BodyDef bodyDef;
    bodyDef.type = b2_dynamicBody;
    bodyDef.position.Set(0.0f, 0.0f);
    b2Body* body = world.CreateBody(&bodyDef);
[box2d_dump.txt](https://github.com/erincatto/box2d/files/12721822/box2d_dump.txt)

    // Define another box shape for the bullet
    b2PolygonShape dynamicBox;
    dynamicBox.SetAsBox(0.06f, 0.019f);

    // Define the dynamic body fixture
    b2FixtureDef fixtureDef;
    fixtureDef.shape = &dynamicBox;
    fixtureDef.density = 0.0f;
    fixtureDef.friction = 0.0f;

    // Add the shape to the body
    body->CreateFixture(&fixtureDef);

    // Create a mass data object to hold the mass of the bullet
    b2MassData massData;
    massData.mass = 0.0650f;
    massData.center = body->GetPosition();
    massData.I = 0.1f;
    body->SetMassData(&massData);
    float mass = massData.mass;

    // Set the initial velocity of the bullet
    float angle = 45.0f; // Angle in degrees
    float rad_angle= angle * (M_PI) / 180.0f ; // Convert an angle to radians
    float speed = 945.0f; // Speed in m/s
    float vx = speed * std::cos(rad_angle);
    float vy = speed * std::sin(rad_angle);
    b2Vec2 prevVelocity(vx, vy);

    body->SetLinearVelocity(prevVelocity);
    body->SetLinearDamping(0.15f);
    std::cout << prevVelocity.x << " " << prevVelocity.y << std::endl;

    // Simulate the world
    float timeStep = 1.0f / 60.0f;
    int32 velocityIterations = 8;
    int32 positionIterations = 8;

    for (int i = 0; i < 60; ++i)
    {
        body->SetLinearVelocity(prevVelocity);
        world.Step(timeStep, velocityIterations, positionIterations);

        // Calculate the kinetic energy of the bullet
        b2Vec2 currentVelocity = body->GetLinearVelocity();
        float linearVelocity = (currentVelocity.x * currentVelocity.x) + (currentVelocity.y * currentVelocity.y);
        linearVelocity = sqrt(linearVelocity);
        float kineticEnergy = 0.5f *(mass * (linearVelocity * linearVelocity));

        std::cout << "Bullet Position: (" << body->GetPosition().x << "," << body->GetPosition().y << ")" << std::endl;
        std::cout << "Bullet Velocity: (" << linearVelocity << ")"<< std::endl;
        std::cout << "Kinetic energy of bullet: " << kineticEnergy <<" " << "Joules" << std::endl;

        prevVelocity = currentVelocity;
    }

    // Remember to clean up
    world.DestroyBody(body);

    return 0;
}
  • Ask for help on discord and/or reddit
  • Consider providing a dump file using b2World::Dump
@BiomedicLJZ
Copy link
Author

Here is the dump file
box2d_dump.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant