You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
#defineM_PI3.14159265358979323846
#endifintmain() {
// 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 bulletfloat angle = 45.0f; // Angle in degreesfloat rad_angle= angle * (M_PI) / 180.0f ; // Convert an angle to radiansfloat speed = 945.0f; // Speed in m/sfloat 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 worldfloat 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);
return0;
}
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:
The text was updated successfully, but these errors were encountered: