Hi everybody 🙂
I noticed a strange behavior of my box, when the body is rotating and when I apply an impluse to it, it steel rotating.
I don't why, this is my code:
/*
* Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#include "Box2D.h"
#include <SFML/Graphics.hpp>
#include <iostream>
#include <cstdio>
#include <cmath>
// This is a simple example of building and running a simulation
// using Box2D. Here we create a large ground box and a small dynamic
// box.
int main(int argc, char** argv)
{
B2_NOT_USED(argc);
B2_NOT_USED(argv);
sf::RenderWindow App(sf::VideoMode(800, 600), "SFML Shapes");
sf::Event Event;
b2AABB worldAABB;
float32 scale=10.0f;
worldAABB.lowerBound.Set(-1000.0f, -1000.0f);
worldAABB.upperBound.Set(1000.0f, 1000.0f);
b2Vec2 gravity(0.0f, 10.0f);
bool doSleep = true;
b2World world(worldAABB, gravity, doSleep);
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0.0f,550/scale);
b2Body* groundBody = world.CreateBody(&groundBodyDef);
b2PolygonDef groundShapeDef;
groundShapeDef.SetAsBox(400.0f/scale, 25.0f/scale);
groundBody->CreateShape(&groundShapeDef);
b2BodyDef bodyDef;
bodyDef.position.Set(0.0f,50.0f/scale);
b2Body* body = world.CreateBody(&bodyDef);
b2BodyDef bodyDef2;
bodyDef2.position.Set(0.0f,100.0f/scale);
b2Body* body2 = world.CreateBody(&bodyDef2);
b2PolygonDef shapeDef;
shapeDef.SetAsBox(10.0f/scale, 10.0f/scale);
b2PolygonDef shapeDef2;
shapeDef2.SetAsBox(10.0f/scale, 10.0f/scale);
shapeDef.density = 1.25f;
shapeDef.friction = 0.3f;
shapeDef2.density = 1.0f;
shapeDef2.friction = 0.3f;
body->CreateShape(&shapeDef);
body->SetMassFromShapes();
body2->CreateShape(&shapeDef2);
body2->SetMassFromShapes();
float32 timeStep = 1.0f / 60.0f;
int32 iterations = 10;
float angle=0.0f;
float angle2=0.0f;
sf::Shape Box=sf::Shape::Rectangle(0, 0, 20, 20, sf::Color::Red);
Box.SetCenter(10,10);
sf::Shape Box2=sf::Shape::Rectangle(0, 0, 20, 20, sf::Color::Green);
Box2.SetCenter(10,10);
while (App.IsOpened())
{
while (App.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)
App.Close();
}
world.Step(timeStep, iterations);
b2Vec2 position = body->GetPosition();
angle=body->GetAngle();
angle2=body2->GetAngle();
b2Vec2 position2 = body2->GetPosition();
if (App.GetInput().IsKeyDown(sf::Key::Left)) body->ApplyForce(b2Vec2(-20,0),b2Vec2(position.x,position.y));
if (App.GetInput().IsKeyDown(sf::Key::Right)) body->ApplyForce(b2Vec2(20,0),b2Vec2(position.x,position.y));
if (App.GetInput().IsKeyDown(sf::Key::Up)) body->ApplyImpulse(b2Vec2(0,-1),b2Vec2(position.x,position.y));
if (App.GetInput().IsKeyDown(sf::Key::Down)) body->ApplyImpulse(b2Vec2(0,1),b2Vec2(position.x,position.y));
if (App.GetInput().IsKeyDown(sf::Key::Q)) body2->ApplyForce(b2Vec2(-20,0),b2Vec2(position2.x,position2.y));
if (App.GetInput().IsKeyDown(sf::Key::D)) body2->ApplyForce(b2Vec2(20,0),b2Vec2(position2.x,position2.y));
if (App.GetInput().IsKeyDown(sf::Key::Z)) body2->ApplyImpulse(b2Vec2(0,-1),b2Vec2(position2.x,position2.y));
if (App.GetInput().IsKeyDown(sf::Key::S)) body2->ApplyImpulse(b2Vec2(0,1),b2Vec2(position2.x,position2.y));
App.Clear();
Box.SetPosition(position.x*scale+10,position.y*scale+10);
Box.SetRotation(-(angle*180)/M_PI);
Box2.SetRotation(-(angle2*180)/M_PI);
Box2.SetPosition(position2.x*scale+10,position2.y*scale+10);
App.Draw(Box);
App.Draw(Box2);
App.Display();
}
return 0;
}
you can compile it,and try to rotate the red box then before it touch anything apply an impluse and you'll see what I'm talking about...
I noticed a strange behavior of my box, when the body is rotating and when I apply an impluse to it, it steel rotating.
I don't why, this is my code:
Code:
/*
* Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#include "Box2D.h"
#include <SFML/Graphics.hpp>
#include <iostream>
#include <cstdio>
#include <cmath>
// This is a simple example of building and running a simulation
// using Box2D. Here we create a large ground box and a small dynamic
// box.
int main(int argc, char** argv)
{
B2_NOT_USED(argc);
B2_NOT_USED(argv);
sf::RenderWindow App(sf::VideoMode(800, 600), "SFML Shapes");
sf::Event Event;
b2AABB worldAABB;
float32 scale=10.0f;
worldAABB.lowerBound.Set(-1000.0f, -1000.0f);
worldAABB.upperBound.Set(1000.0f, 1000.0f);
b2Vec2 gravity(0.0f, 10.0f);
bool doSleep = true;
b2World world(worldAABB, gravity, doSleep);
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0.0f,550/scale);
b2Body* groundBody = world.CreateBody(&groundBodyDef);
b2PolygonDef groundShapeDef;
groundShapeDef.SetAsBox(400.0f/scale, 25.0f/scale);
groundBody->CreateShape(&groundShapeDef);
b2BodyDef bodyDef;
bodyDef.position.Set(0.0f,50.0f/scale);
b2Body* body = world.CreateBody(&bodyDef);
b2BodyDef bodyDef2;
bodyDef2.position.Set(0.0f,100.0f/scale);
b2Body* body2 = world.CreateBody(&bodyDef2);
b2PolygonDef shapeDef;
shapeDef.SetAsBox(10.0f/scale, 10.0f/scale);
b2PolygonDef shapeDef2;
shapeDef2.SetAsBox(10.0f/scale, 10.0f/scale);
shapeDef.density = 1.25f;
shapeDef.friction = 0.3f;
shapeDef2.density = 1.0f;
shapeDef2.friction = 0.3f;
body->CreateShape(&shapeDef);
body->SetMassFromShapes();
body2->CreateShape(&shapeDef2);
body2->SetMassFromShapes();
float32 timeStep = 1.0f / 60.0f;
int32 iterations = 10;
float angle=0.0f;
float angle2=0.0f;
sf::Shape Box=sf::Shape::Rectangle(0, 0, 20, 20, sf::Color::Red);
Box.SetCenter(10,10);
sf::Shape Box2=sf::Shape::Rectangle(0, 0, 20, 20, sf::Color::Green);
Box2.SetCenter(10,10);
while (App.IsOpened())
{
while (App.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)
App.Close();
}
world.Step(timeStep, iterations);
b2Vec2 position = body->GetPosition();
angle=body->GetAngle();
angle2=body2->GetAngle();
b2Vec2 position2 = body2->GetPosition();
if (App.GetInput().IsKeyDown(sf::Key::Left)) body->ApplyForce(b2Vec2(-20,0),b2Vec2(position.x,position.y));
if (App.GetInput().IsKeyDown(sf::Key::Right)) body->ApplyForce(b2Vec2(20,0),b2Vec2(position.x,position.y));
if (App.GetInput().IsKeyDown(sf::Key::Up)) body->ApplyImpulse(b2Vec2(0,-1),b2Vec2(position.x,position.y));
if (App.GetInput().IsKeyDown(sf::Key::Down)) body->ApplyImpulse(b2Vec2(0,1),b2Vec2(position.x,position.y));
if (App.GetInput().IsKeyDown(sf::Key::Q)) body2->ApplyForce(b2Vec2(-20,0),b2Vec2(position2.x,position2.y));
if (App.GetInput().IsKeyDown(sf::Key::D)) body2->ApplyForce(b2Vec2(20,0),b2Vec2(position2.x,position2.y));
if (App.GetInput().IsKeyDown(sf::Key::Z)) body2->ApplyImpulse(b2Vec2(0,-1),b2Vec2(position2.x,position2.y));
if (App.GetInput().IsKeyDown(sf::Key::S)) body2->ApplyImpulse(b2Vec2(0,1),b2Vec2(position2.x,position2.y));
App.Clear();
Box.SetPosition(position.x*scale+10,position.y*scale+10);
Box.SetRotation(-(angle*180)/M_PI);
Box2.SetRotation(-(angle2*180)/M_PI);
Box2.SetPosition(position2.x*scale+10,position2.y*scale+10);
App.Draw(Box);
App.Draw(Box2);
App.Display();
}
return 0;
}
you can compile it,and try to rotate the red box then before it touch anything apply an impluse and you'll see what I'm talking about...