How do I scale Box2D objects with PPM? - c++

I recently ran into a problem with Box2D where my player was moving at a slow speed. I use LinearImpulse's to move the body but (100, 100) was the fastest it could go.
After researching it a bit, I know that the Box2D body is too large because Box2D uses meters and I'm typing it in pixels. I know I have to scale the bodies using a PPM constant, but it's not quite making sense to me.
I've created a PPM constant and set it to 32.0f.
const float PPM = 32.0f;
Here's how I create the dynamic player body:
void Physics::playerDynamic(float x, float y, float width, float height)
{
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(x / PPM, y / PPM);
body = world.CreateBody(&bodyDef);
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox((64.0f) / PPM, (64.0f) / PPM);
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.filter.categoryBits = CATEGORY_PLAYER;
fixtureDef.filter.maskBits = CATEGORY_PLAYER;
fixtureDef.filter.groupIndex = -1;
body->CreateFixture(&fixtureDef);
world.Step(timeStep, velocityIterations, positionIterations);
posX = x;
posY = y;
bodyType = "player";
}
I understand that I need to scale down the body, which I did by dividing it by the PPM. But now the body is really small and won't collide how I'd like it to.
Here is the function where I apply the impulse to the player body:
void Physics::moveBodies()
{
myCollisionDetection myListener;
world.SetContactListener(&myListener);
if (bodyType != "player")
{
}
else if (bodyType == "player")
{
body->SetAwake(true);
float forceX = 0;
float forceY = 0;
switch (moveState)
{
case MS_UP:
forceY = -50;
break;
case MS_DOWN:
forceY = 50;
break;
case MS_LEFT:
forceX = -50;
break;
case MS_RIGHT:
forceX = 50;
break;
case MS_UPRIGHT:
forceY = -50;
forceX = 50;
break;
case MS_UPLEFT:
forceY = -50;
forceX = -50;
break;
case MS_DOWNRIGHT:
forceY = 50;
forceX = 50;
break;
case MS_DOWNLEFT:
forceY = 50;
forceX = -50;
break;
case MS_STOP:
body->SetLinearVelocity(b2Vec2(0, 0));
}
body->ApplyLinearImpulse(b2Vec2(forceX, forceY ), body->GetWorldCenter(), true);
pos = body->GetPosition();
posX = body->GetPosition().x;
posY = body->GetPosition().y;
}
world.Step(timeStep, velocityIterations, positionIterations);
}
I've been researching this for quite a while and it seems simple but I'm having a hard time applying it to my code.
So what do I do from here? Do I scale the body back up?
I'm also using SDL2 for the graphics. Do I need to change anything with the rendering?

Related

Box2D - Firing bullet from rotating gun

i have a little trouble as title said: i can't figure out how to shoot the bullet toward the direction the gun is pointing at.
Here's short version code, for my bullet firing:
b2BodyDef bd;
bd.type = b2_dynamicBody;
bd.bullet = true;
bd.position = gun->GetPosition();//bullet start at the middle of the gun
m_bullet = m_world->CreateBody(&bd);
m_bullet->CreateFixture(&fd);
m_bullet->ApplyLinearImpulseToCenter( ??? ,true);
At first i thought first param is the direction you want the body go toward so i put in gun->GetWorldPoint(b2Vec2(0.0f,-5.0f)) (middle of the gun's muzzle). Big miss! After a while i thought i should try put in the vector of current gun's rotation degrees b2Vec2 vect = b2Vec2(cos(angle * PI/180), sin(angle * PI/180)); but then the bullet won't fly at all. Now i am all out of ideas. Please, some light.
Full version of code:
public:
b2Body* m_bullet = NULL;
b2Body* gun;
b2RevoluteJoint* joint1;
b2FixtureDef fd;
TestingStuff()
{
{
//body
b2CircleShape circle1;
circle1.m_radius = 1.6f;
fd.shape = &circle1;
fd.density = 1.0f;
fd.filter.groupIndex = -1;
b2BodyDef bd1;
bd1.type = b2_staticBody;
bd1.position.Set(-5.0f, 9.0f);
b2Body* body1 = m_world->CreateBody(&bd1);
body1->CreateFixture(&fd);
//gun
b2PolygonShape box;
box.SetAsBox(0.5f, 5.0f);
fd.shape = &box;
fd.density = 1.0f;
fd.filter.groupIndex = -1;
b2BodyDef bd2;
bd2.type = b2_dynamicBody;
bd2.position.Set(-5.0f, 8.0f);
gun = m_world->CreateBody(&bd2);
gun->CreateFixture(&fd);
//joint
b2RevoluteJointDef jd1;
jd1.Initialize(gun, body1, bd1.position);
jd1.enableMotor = true;
jd1.maxMotorTorque = 90;
jd1.motorSpeed = 180 * DEGTORAD;//DEGTORAD=0.0174532925199432957f
joint1 = (b2RevoluteJoint*) m_world->CreateJoint(&jd1);
}
}
void Keyboard(int key)
{
switch (key)
{
case GLFW_KEY_COMMA:
if (m_bullet != NULL)
{
m_world->DestroyBody(m_bullet);
m_bullet = NULL;
}
{
//bullet
b2CircleShape shape;
shape.m_radius = 0.25f;
fd.shape = &shape;
fd.density = 1;
fd.restitution = 0.05f;
fd.filter.groupIndex = -1;
b2BodyDef bd;
bd.type = b2_dynamicBody;
bd.bullet = true;
bd.position = gun->GetPosition();
m_bullet = m_world->CreateBody(&bd);
m_bullet->CreateFixture(&fd);
m_bullet->ApplyLinearImpulseToCenter( ??? ,true);
}
break;
}
}
After a good sleep i found the solution after few more trial and error.
//bullet
float degAngle = joint1->GetJointAngle() * RADTODEG + 180;
b2Vec2 vect = b2Vec2(sin(degAngle* (b2_pi / 180)) * 10, cos(degAngle* (b2_pi / 180)) * 10);
m_bullet->ApplyLinearImpulseToCenter(vect ,true);
The * 10 is to increase the impulse, make the bullet fly faster and farther, for the sake of testing i just make it 10. Notice that this code is just for testing, if you want to make it more realistic, increase the impulse as well as make the bullet go toward the muzzle instead of go toward the vector it had been fired at.

OpenGL First person camera

I'm trying to do a first cam person using OPENGL. But here's my problem.
Let me introduce you my code.
First of all, I have this function that allows me to get the mouse X and Y position:
case WM_MOUSEMOVE:
CameraManager.oldMouseX = CameraManager.mouseX;
CameraManager.oldMouseY = CameraManager.mouseY;
CameraManager.mouseX = GET_X_LPARAM(lParam);
CameraManager.mouseY = GET_Y_LPARAM(lParam);
mousePoint.x = CameraManager.mouseX - CameraManager.oldMouseX;
mousePoint.y = CameraManager.mouseY - CameraManager.oldMouseY;
mousePoint.z = 0.f;
CameraManager.lookAt(mousePoint);
App.onRender();
break;
Here I get the difference between the old mouse position and the new one (just because I want to know when I have to increase/decrease the angle). Then, I call the lookAt function on my CameraManager.
Here I do the following:
if (point.x > 0.f) {
camAngle.x -= 0.3f;
}
else if (point.x < 0.f) {
camAngle.x += 0.3f ;
}
float radiansX = MathUtils::CalculateRadians(camAngle.x);
//eye.x = sinf(radiansX);
//center.x += sinf(radiansX);
//center.z += (-cosf(radiansX));
Update();
And then my update does:
glLoadIdentity();
gluLookAt(eye.x, eye.y, eye.z,
center.x, center.y, center.z,
up.x, up.y, up.z);
I've read a lot of stuff on how to refresh the eye and center, but I couldn't get anything to work.
Any advices?

GLUT timer function

I am trying to get my tokens on a board game to fall slowly. Right now, they fall, but they fall so fast. How could I implement the timer function in my code? Right now I do a loop, that updates the y coordinate of glTranslate. But it is still too fast! the top y is the y coordinate where I press on the screen, and the bottomy is the coordinates of the lowest open spot for a token.
col =0;
double bottomy = 0;
int row = 0;
circlex = (double)x / width ;
circley = (double)y / height ;
row = board.getRow(col) + 1;
bottomy = 500 - (25*row);
for( double topy = y ; topy <= bottomy; topy += 2 ){
glTranslatef(circlex, circley, 0.0f);
circley += .0000000000000000001;
display();
}
r = board.makeMove(col);
You can use glutTimerFunc to execute a function at a regular time period. This has the signature
void glutTimerFunc(unsigned int msecs,
void (*func)(int value),
value);
For example if your drawing function was
void UpdateTokens(int time);
Then you could call an update every 0.5 seconds with the following call (where current_time was the current simulation time)
glutTimerFunc(500, UpdateTokens, current_time);
For more precise timing, I would recommend using <chrono> instead, and performing your timing using things like std::chrono::duration with a std::chrono::steady_clock.
The actual problem here is how glut works. Basically, the user only gets a image presented at the end of the main loop. As long as you do not return from the mouse function, nothing is presented on screen. You can solve the problem by transferring the work to the display function and distribute the translation across multiple frames:
global variables:
double circlex = 0, circley = 0, bottomy = 0;
bool isfalling = false;
int topy = 0;
mouse_func:
if (isfalling == false) //Prevents the user from clicking during an animation
{
circlex = (double)x / width ;
circley = (double)y / height ;
int row = board.getRow(col) + 1;
bottomy = 500 - (25*row);
topy = y;
isfalling = true;
}
display_func:
if (isfalling)
{
circley += .0000000000000000001;
topy += 2;
if (topy >= bottomy)
isfalling = false;
}
glTranslatef(circlex, circley, 0.0f);
display();

Detect CCSprite touch in small distance between them

I have some small sprites (width/height) in a CCLayer and i want to detect which of them is touched. I use the following code.
- (BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
CGRect rect = sprite.boundingBox;
// i am doing this because of the small size of the sprites,
// so it would be more easy to detect if a sprite is taped and then move it.
if (rect.size.width > rect.size.height) {
rect.size.width *= 2.5;
rect.size.height *= 5;
rect.origin.y -= rect.size.height / 2;
} else {
rect.size.width *= 5;
rect.size.height *= 2.5;
rect.origin.x -= rect.size.width / 2;
}
CCSprite *s = nil;
for (CCSprite *sprite in [self children]) {
if (CGRectContainsPoint(rect, touchPoint)) {
s = sprite;
break;
}
}
if (s != nil) {
// do something here
}
return YES;
}
All works fine except when two sprites is very close (not overlap) to each other. Then because of the small distance between them, the wrong sprite is detected.
Any idea how could i correct it?
If you are trying to enlarge the rects keeping their center where it originally is, you should use:
rect.origin.y -= (rectFinalHeight - rectOriginalHeight) / 2;
and
rect.origin.y -= (rectFinalWidth - rectOriginalWidth) / 2;
which would be:
if (rect.size.width > rect.size.height) {
rect.size.width *= 2.5;
rect.size.height *= 5;
rect.origin.y -= rect.size.height * 4 / 2;
} else {
rect.size.width *= 5;
rect.size.height *= 2.5;
rect.origin.x -= rect.size.width * 4 / 2;
}
IMO, you should adjust the rect origin along both axis in both cases (and not only in one case along the x axis and in the other along the y axis), otherwise your rect will not be centered. But you understand better what you are trying to do, here...

Openscenegraph - How to create an invisible boundary beyond which camera does not go

I am new to 3d programming so here goes. I am trying to simulate a room. I don't have images for the walls loaded but I want to in code simulate the boundaries. How do I accomplish this please?
Below is the code that handles the movement of the camera
bool calcMovement()
{
// return if less then two events have been added.
if (_ga_t0.get()==NULL || _ga_t1.get()==NULL) return false;
//calcIntersect();
double dt = _ga_t0->getTime()-_ga_t1->getTime();
if (dt<0.0f)
{
OSG_INFO << "warning dt = "<<dt<< std::endl;
dt = 0.0;
}
double accelerationFactor = _height*10.0;
switch(_speedMode)
{
case(USE_MOUSE_Y_FOR_SPEED):
{
double dy = _ga_t0->getYnormalized();
_velocity = _height*dy;
break;
}
case(USE_MOUSE_BUTTONS_FOR_SPEED):
{
unsigned int buttonMask = _ga_t1->getButtonMask();
//add cases here for finding which key was pressed.
if (buttonMask==GUIEventAdapter::LEFT_MOUSE_BUTTON || wPressed)
{
// pan model.
_velocity += dt*accelerationFactor;
}
else if (buttonMask==GUIEventAdapter::MIDDLE_MOUSE_BUTTON ||
buttonMask==(GUIEventAdapter::LEFT_MOUSE_BUTTON|GUIEventAdapter::RIGHT_MOUSE_BUTTON))
{
_velocity = 0.0;
}
else if (buttonMask==GUIEventAdapter::RIGHT_MOUSE_BUTTON || sPressed)
{
_velocity -= dt*accelerationFactor;
}
break;
}
}
osg::CoordinateFrame cf=getCoordinateFrame(_eye);
osg::Matrixd rotation_matrix;
rotation_matrix.makeRotate(_rotation);
osg::Vec3d up = osg::Vec3d(0.0,1.0,0.0) * rotation_matrix;
osg::Vec3d lv = osg::Vec3d(0.0,0.0,-1.0) * rotation_matrix;
osg::Vec3d sv = osg::Vec3d(1.0,0.0,0.0) * rotation_matrix;
// rotate the camera.
double dx = _ga_t0->getXnormalized();
double yaw = -inDegrees(dx*50.0*dt);
#ifdef KEYBOARD_PITCH
double pitch_delta = 0.5;
if (_pitchUpKeyPressed) _pitch += pitch_delta*dt;
if (_pitchDownKeyPressed) _pitch -= pitch_delta*dt;
#endif
#if defined(ABOSULTE_PITCH)
// absolute pitch
double dy = _ga_t0->getYnormalized();
_pitch = -dy*0.5;
#elif defined(INCREMENTAL_PITCH)
// incremental pitch
double dy = _ga_t0->getYnormalized();
_pitch += dy*dt;
#endif
osg::Quat yaw_rotation;
yaw_rotation.makeRotate(yaw,up);
_rotation *= yaw_rotation;
rotation_matrix.makeRotate(_rotation);
sv = osg::Vec3d(1.0,0.0,0.0) * rotation_matrix;
wPressed = false;
sPressed = false;
// movement is big enough the move the eye point along the look vector.
if (fabs(_velocity*dt)>1e-8)
{
double distanceToMove = _velocity*dt;
double signedBuffer;
if (distanceToMove>=0.0) signedBuffer=_buffer;
else signedBuffer=-_buffer;
// check to see if any obstruction in front.
osg::Vec3d ip, np;
if (intersect(_eye,_eye+lv*(signedBuffer+distanceToMove), ip, np))
{
if (distanceToMove>=0.0)
{
distanceToMove = (ip-_eye).length()-_buffer;
}
else
{
distanceToMove = _buffer-(ip-_eye).length();
}
_velocity = 0.0;
}
// check to see if forward point is correct height above terrain.
osg::Vec3d fp = _eye + lv*distanceToMove;
osg::Vec3d lfp = fp - up*(_height*5.0);
if (intersect(fp, lfp, ip, np))
{
if (up*np>0.0) up = np;
else up = -np;
_eye = ip+up*_height;
lv = up^sv;
computePosition(_eye,_eye+lv,up);
return true;
}
// no hit on the terrain found therefore resort to a fall under
// under the influence of gravity.
osg::Vec3d dp = lfp;
dp -= getUpVector(cf)* (2.0*_modelScale);
if (intersect(lfp, dp, ip, np))
{
if (up*np>0.0) up = np;
else up = -np;
_eye = ip+up*_height;
lv = up^sv;
computePosition(_eye,_eye+lv,up);
return true;
}
// no collision with terrain has been found therefore track horizontally.
lv *= (_velocity*dt);
_eye += lv;
}
return true;
}