6 Replies - 4274 Views - Last Post: 13 April 2012 - 08:02 AM Rate Topic: -----

#1 commodorejim  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 04-January 12

SDL and Box2D - A terrain's slope angle and rendering a car sprite

Posted 10 April 2012 - 06:27 AM

I'm in the middle of messing around with Box2D using SDL for rendering. At the moment, I'm trying out a simple horizontally scrolling demo of a cart body with two wheels moving along some terrain. Things are working okay so far but I've hit a bit of a brick wall regarding slopes.

Essentially if the cart starts to move up a slope, the cart's Box2D body should rotate to the slope's angle and I'd like to render the cart's sprite at this angle. I've installed the sdl_gfx lib and it's working fine but I'm uncertain as to how to tie these elements together. Is it a case of passing the slope's angle to the cart body? I've tried this already but it draws the cart sprite at this angle constantly and, obviously, I only want it drawn at this angle when it's going up (or down) the slope. Can anyone give me some pointers? I can post any relevant code if needed.

Is This A Good Question/Topic? 0
  • +

Replies To: SDL and Box2D - A terrain's slope angle and rendering a car sprite

#2 Toadill  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 392
  • Joined: 08-January 12

Re: SDL and Box2D - A terrain's slope angle and rendering a car sprite

Posted 10 April 2012 - 07:01 AM

So basically you need to find the slope
http://www.learningw...on2/slope3.html

You will have to show some code as it would help to explain a few things.

This post has been edited by Toadill: 10 April 2012 - 07:05 AM

Was This Post Helpful? 0
  • +
  • -

#3 commodorejim  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 04-January 12

Re: SDL and Box2D - A terrain's slope angle and rendering a car sprite

Posted 10 April 2012 - 12:20 PM

Hi Toadill, thanks for your reponse. I'm not sure which code I should post (bear in mind I'm a total novice) but the following might help:

The following is the Box2D code for the cart's body:

   b2BodyDef cartDef;
   cartDef.position.Set(3.9, 18.82);
   cartDef.type = b2_dynamicBody;
   cartDef.angle = 30.0f;
   cart = world->CreateBody(&cartDef);

   b2PolygonShape cartShape;
   cartShape.SetAsBox(2.00/PTM_RATIO, 1/PTM_RATIO);
   b2FixtureDef cartFixture;
   cartFixture.density = 0.0;
   cartFixture.friction = 0.0;
   cartFixture.restitution = 0.2;
   cartFixture.filter.groupIndex = -1;
   cartFixture.shape = &cartShape;
   cart->CreateFixture(&cartFixture);



Then we have the Box2D code for the wheels:

 b2BodyDef wheelDef;
   wheelDef.type = b2_dynamicBody;
   wheelDef.position.Set(4.0, 19.5);
   wheel1 = world->CreateBody(&wheelDef);
   
   wheelDef.position.Set(6.0, 19.5);
   wheelDef.angle = 30.0f;

   wheel2 = world->CreateBody(&wheelDef);

   b2CircleShape wheelShape;
   wheelShape.m_radius = 0.95;

   b2FixtureDef wheelFixture;
   wheelFixture.density = 1.1;
   wheelFixture.friction = 5;
   wheelFixture.restitution = 0.0;
   wheelFixture.filter.groupIndex = -1;
   wheelFixture.shape = &wheelShape;

   wheel1->CreateFixture(&wheelFixture);
   wheel2->CreateFixture(&wheelFixture);

   b2RevoluteJointDef wheelJointDef;
   wheelJointDef.enableMotor = true;
   wheelJointDef.maxMotorTorque = 50;
   
   wheelJointDef.Initialize(wheel1, cart, wheel1->GetWorldCenter());
   motor1 = (b2RevoluteJoint*)(world->CreateJoint(&wheelJointDef));

   wheelJointDef.Initialize(wheel2, cart, wheel2->GetWorldCenter());
   motor2 = (b2RevoluteJoint*)(world->CreateJoint(&wheelJointDef));


Finally we have the code for the slope:

   b2BodyDef bd;
	bd.position.Set(15.0f, 19.50f);
	bd.type = b2_dynamicBody;
	slopebody = world->CreateBody(&bd);

	b2PolygonShape box;
	box.SetAsBox(8.0f, 2.25f, b2Vec2(0, 0), -0.25);  //	box.SetAsBox(3.0f, 0.25f, b2Vec2(0, 0), -0.25);
	slopebody->CreateFixture(&box, 1.0f);

	b2RevoluteJointDef jd;
	jd.Initialize(groundBody, slopebody, slopebody->GetPosition());
	jd.lowerAngle = -8.0f * b2_pi / 180.0f;
	jd.upperAngle = 8.0f * b2_pi / 180.0f;
	jd.enableLimit = true;
	world->CreateJoint(&jd);

	slopebody->ApplyAngularImpulse(100.0f);



That's all the Box2D definitions.

Now in SDL, I'm declaring the following vectors for the cart body, the wheels and the slope.


b2Vec2 bikebodypos = cart->GetPosition();
b2Vec2 wheel1pos = wheel1->GetPosition();
b2Vec2 wheel2pos = wheel2->GetPosition();
b2Vec2 slopepos = slopebody->GetPosition();


I'm then calling a function called apply_surface() and passing it the relevant arguments for rendering the sprites at the above bodies' locations, e.g.

apply_surface((wheel1pos.x*PTM_RATIO)-camera.x,wheel1pos.y*PTM_RATIO, rotate_wheel_surface, screen_surface, 0, 0.0);


Finally in the apply_surface() function, I'm doing the following:

float wheelangle = wheel1->GetAngle(); 
rotate_wheel_surface = rotate(wheel_surface,wheelangle, 1, 0);


Phew, that's a lot of code - sorry. But I'm trying to give as comprehensive an overview as I can. Hope it's not overkill. Thanks for any help you can offer.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 3992
  • View blog
  • Posts: 12,321
  • Joined: 25-December 09

Re: SDL and Box2D - A terrain's slope angle and rendering a car sprite

Posted 10 April 2012 - 12:26 PM

This does not seem to really be a C/C++ question. Moving to the games Forum.

Jim

This post has been edited by jimblumberg: 10 April 2012 - 12:27 PM

Was This Post Helpful? 0
  • +
  • -

#5 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: SDL and Box2D - A terrain's slope angle and rendering a car sprite

Posted 10 April 2012 - 03:46 PM

There are a number of issues that I can see with your code. The first is that you are over-complicating things here:

b2Vec2 bikebodypos = cart->GetPosition();
b2Vec2 wheel1pos = wheel1->GetPosition();
b2Vec2 wheel2pos = wheel2->GetPosition();
b2Vec2 slopepos = slopebody->GetPosition();



The various parts of your car should be part of the same 'model' all referencing the same vector, in that you have a point in the center of your model that remains parallel to the surface, so you should be having something like:

b2Vec2 bikebodypos = cart->GetOrientation();



Notice that I've changed position to orientation? Recall that a vector's position is conceptual and its orientation relative, in this case on the surface on which the car rests.

In order to correct the orientation of the car body relative to the surface (ground), you could use the vector product (also known as the 'cross product') to give you the perpendicular vector to the surface. There is one catch though; the vector product only functions correctly in 3 dimensions, in that it cannot be used to work out the perpendicular line to the horizontal in 2D space. You can have a conceptual third dimension as a z-vector norm (unit vector), then strip it when using it in your 2D engine.

Now for the car model, if it is going to be articulated it is going to mean a lot more work for you as you will have to consider spring forces (Google "Hooke's Law"), which will increase overall code complexity exponentially unless Box2D provides functionality for this.

I would suggest that you include logic that keeps each component of the car model relative to the overall model's origin, rather than the ground. It won't look entirely convincing, but it should still look like a good attempt.

This post has been edited by ButchDean: 10 April 2012 - 03:50 PM

Was This Post Helpful? 1
  • +
  • -

#6 commodorejim  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 04-January 12

Re: SDL and Box2D - A terrain's slope angle and rendering a car sprite

Posted 13 April 2012 - 07:55 AM

Great! Thanks so much for your help. Looks like I've a lot of work to do but your suggestions have been extremely helpful.
Was This Post Helpful? 0
  • +
  • -

#7 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: SDL and Box2D - A terrain's slope angle and rendering a car sprite

Posted 13 April 2012 - 08:02 AM

No problem. Enjoy the project! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1