Box2D Forums

It is currently Mon Sep 06, 2010 10:06 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Having trouble
PostPosted: Wed Jan 21, 2009 2:34 pm 
Offline

Joined: Mon Jan 19, 2009 12:44 am
Posts: 56
I am having to apply forces of up to 500 to see any considerable motion, is this normal?

If apply two consecutive forces do they accumulate?

Does damping affect the body's overall motion? I assume linear works on forces(velocity) and angular works on torque(rotation)?

How do you make an object stop/slow down until stop after applying a force? I've tried with damping but I haven't been able to get it to work properly(It seems to slow the object's overall movement). Trying to create an asteroids like game.


Thanks


Top
 Profile  
 
 Post subject: Re: Having trouble
PostPosted: Thu Jan 22, 2009 6:54 am 
Offline

Joined: Sun Jan 04, 2009 11:03 am
Posts: 48
No need to open a new thread for your questions, you can ask new questions in the old ones.

Forces are applied over time, impulses are applied instantly. If you want an instant effect, use impulses. Also, are you sure you're scaling everything?

Forces accumulate, yes.

linearDamping effects velocity, angularDamping effects angles.

To your question about slowing an object, please refer to Newton. "An object in motion stays in motion" You could "simulate" air-resitance by using damping.

For an astroids like game, create a world boundary bigger then the play area. When an asteroid falls out of the play area by not the world boundary, warp the object to the opposite axises of the play area, so it will fall back into view in the play area. In asteroids games, you will notice that objects once set in motion, never stop, you need to apply another force to counteract the original or to change direction.


Top
 Profile  
 
 Post subject: Re: Having trouble
PostPosted: Thu Jan 22, 2009 7:06 am 
Offline

Joined: Mon Jan 19, 2009 12:44 am
Posts: 56
Alright sorry.

About scaling, that's something I don't understand that I came across in the manual. What do I have to scale? The objects drawn?(But then everything will look big).

So damping is the only way to make an object slow over time?(I am aware that most asteroid games use the teleportation method but I don't want to use that approach on my game)


Top
 Profile  
 
 Post subject: Re: Having trouble
PostPosted: Thu Jan 22, 2009 7:43 am 
Offline

Joined: Sun Jan 04, 2009 11:03 am
Posts: 48
Before you get to far into things, implement scaling.

You need to scale down your screen coordinates before passing them to box2d functions, and scale up the outputed coordinates. A factor of 100 will probably work out for you. In other words, multiply your corodinates and sizes by 0.01 before passing them to box2d, and when you get the position or vertices to draw, multiply by 100 on each of them.

Box2d is tuned for meters. Without scaling, a 10X10 pixel box (tiny) in Box2d is considered 10mX10m (big). After scaling, a 10X10 pixel box is considered 0.1mX0.1m, both of which are tiny.

What alternative method do you propose over the teleportation one, and why do you feel it would work better?


Top
 Profile  
 
 Post subject: Re: Having trouble
PostPosted: Thu Jan 22, 2009 8:28 am 
Offline

Joined: Mon Jan 19, 2009 12:44 am
Posts: 56
Well it's not going to be a copy of asteroids it'll be quite different and I just don't like the idea of teleportation.

I've noticed that increasing the radius of a body's shape reduces the speed of the object's movement and rotation/

This is the code I use for the creation of the b2Body, its drawing and the changing of its properties(moving). I understand what you're saying although I don't know how to implement it :S.

Object is a class that contains all the data concerning the game objects. 'object_body' is a pointer to a b2Body and a member of the object class. Sprite_instance is a member of the object class containing the a pointer to the texture and the size of the texture being drawn.

Code:
//Body creation
b2Body* Game::create_body(b2Vec2 position, float rotation, float mass, float size)
{
   //Define body
   b2BodyDef bodyDef;
   //Properties
   bodyDef.position.Set(position.x, position.y);  //body's position
   bodyDef.angle = rotation * b2_pi; //body's angle in radians(?)
   bodyDef.massData.mass = 2.0; //body's mass in kg
   //Defaults
   bodyDef.massData.center.SetZero(); //center of mass in local coordinates
   bodyDef.massData.I = 3.0f; //rotational inertia in kg*m^2
   bodyDef.linearDamping = 0.0;
   bodyDef.angularDamping = 0.01f;
   bodyDef.allowSleep = true;
   bodyDef.isSleeping = false;
   bodyDef.isBullet = false;
   //Create body
   b2Body* body = world->CreateBody(&bodyDef);

   //Define shape
   b2CircleDef shapeDef;
   //Properties
   shapeDef.restitution = 1.0f;
   shapeDef.radius = 1.0f;
   shapeDef.localPosition.SetZero();
   //Default
   shapeDef.density = 1.0f;
   shapeDef.friction = 0.3f;
   //Create shape
   body->CreateShape(&shapeDef);
   body->SetMassFromShapes();

   return body;
}
//Body creation call
void Game::create_ship(b2Vec2 position, float rotation)
{
   Object *object_pointer = new Ship(*application);
   object_pointer->object_body = create_body(position, rotation, 3.0f, 1.5f);

   add_queue.push_back(object_pointer);
}
//Drawing function
void Direct3D::draw_sprite(Object &object)
{
   SpriteInstance sprite_instance = object.get_sprite_instance();
   LPDIRECT3DTEXTURE9 texture = sprite_instance.get_sprite()->get_texture();
   
   D3DXVECTOR2 scaling = get_scaling();
   D3DXVECTOR2 center = get_center((float)sprite_instance.get_size() / 2);

   float rotation = object.object_body->GetAngle();
   D3DXVECTOR2 position;
   position.x = object.object_body->GetPosition().x;
   position.y = object.object_body->GetPosition().y;

   RECT frame = get_frame(sprite_instance);

   D3DXMATRIX mat;
   D3DXMatrixTransformation2D(&mat,
                        NULL,
                        NULL,
                        &scaling,
                        &center,
                        rotation,
                        &position);
   
   d3d_sprite->SetTransform(&mat);
    d3d_sprite->Draw(texture, &frame, NULL, NULL, 0xFFFFFFFF);
}
//Move function
void Object::move(float magnitude)
{
   b2Vec2 direction;
   direction.x = sin(-object_body->GetAngle()) * magnitude;
   direction.y = cos(-object_body->GetAngle()) * magnitude;

   object_body->WakeUp();
   object_body->ApplyForce(direction, object_body->GetPosition());
}

//Object update function
void Ship::update(Game &game)
{
   if(game.get_application()->input.is_down(MOVE_FORWARD))
   {
      move(-200);
   }

}


Top
 Profile  
 
 Post subject: Re: Having trouble
PostPosted: Thu Jan 22, 2009 11:43 am 
Offline

Joined: Sun Jan 04, 2009 11:03 am
Posts: 48
I'm not trying to be a jerk here, but it might behove you to take a highschool level physics class.

Force = Mass * Acceleration. If you increase Mass, and Force remains the same, Acceleration decreases. Of course increase a shapes radius and calling SetMassFromShapes() is going to change the mass of the body, that function recomputes the mass based on the density and the shapes attached. If you want your manually set mass to take effect, don't call this function. But typically, you are better off setting the density, shapes, and calling this function, since Box2D is going to produce a mass value that simulates better.

A cursory glance of your code reveals the following:

1. When converting from degrees to radians, multiply by pi/180, not pi.
2. Don't set mass, inertia, or center of gravity, you'll get a more realistic simulation by avoiding massData and setting the density, shapes, and calling SetMassFromShapes()
3. Line 7 in that code block is where you want to multiply the position by whatever scaling constant.
4. Is shapeDef.radius = 1.0f; in meters, pixels, or what? Things will look a lot better if you set the radius based on the actual size of the graphic representing it multiplied by the scaling constant.
5. In your drawing function, multiply the corodinate positions by 1 over your scaling constant.


Top
 Profile  
 
 Post subject: Re: Having trouble
PostPosted: Thu Jan 22, 2009 2:29 pm 
Offline

Joined: Mon Jan 19, 2009 12:44 am
Posts: 56
Yeah I'll learn some physics I've run into several problems due to my lack of understanding.

Thanks a lot for the info!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: Erin Catto, ewjordan, Exabot [Bot], Yahoo [Bot] and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group