Sign in to follow this  
LeftyGuitar

Can't get Iterator to work correctly

Recommended Posts

Hello,

 

I am trying to make it where multiple boxes fall down in a physics simulation. I'm using SFML 2 and Box2D. I managed to get most of it down. However when I run the program, the box just stays up in the air, it doesen't do anything. Any help as to why? Note: That I only have posted the code that I think needs to be looked at. I can post more if it is needed.

sf::RectangleShape box3;

b2PolygonShape dynamicBox3;
b2FixtureDef fixtureDef3;
//set up initial variables
vector <b2FixtureDef> fixtures;
vector <b2Body*> bodies;
vector <sf::RectangleShape> shapes;

box3.setPosition(sf::Vector2f(100, 5));
	box3.setSize(sf::Vector2f(30, 30));
	box3.setFillColor(sf::Color(0, 0, 255));

	shapes.push_back(box3);

bodyDef3.type = b2_dynamicBody;
	ballVec3.Set(30, 30);
	bodyDef3.angularVelocity = 0.0f;
	bodyDef3.linearVelocity = ballVec3;

bodyDef3.position.Set(50, 0);
	bodyDef3.awake = true;
	Body3 = World->CreateBody(&bodyDef3);

dynamicBox3.SetAsBox(10.0f, 10.0f);

fixtureDef3.shape = &dynamicBox3;
	fixtureDef3.density = 2.0f;
	fixtureDef3.friction = 2.0f;
	fixtureDef3.restitution = .05f;

	Body3->CreateFixture(&fixtureDef3);

	fixtures.push_back(fixtureDef3);

	bodies.push_back(Body3);

//update section
	for (int i = 0; i < bodies.size(); i++)
	{
		b2Vec2 pos3 = bodies[i]->GetPosition();
		float angle3 = bodies[i]->GetAngle();

		box3.setPosition(pos3.x, pos3.y);
		box3.setRotation(angle3);
	}

//Drawing section
for (vector<sf::RectangleShape>::iterator it = shapes.begin(); it != shapes.end(); it++)
	{
		GameWin.draw(*it);
	}

Share this post


Link to post
Share on other sites
When you step through it with the debugger, does it show you what you expect it to show you?

From your copy/paste job, I'm not sure if you included the part where the positions and angles actually get modified. I can see you're reading them from one place and assigning them elsewhere, but I don't know if those values are the ones you actually intended or if you left that off from the code snippets.

Share this post


Link to post
Share on other sites

OK I managed to get it to move, but now it just falls to the ground after a couple of bounces.

for (vector<sf::RectangleShape>::iterator it = shapes.begin(); it != shapes.end(); it++)
	{
		GameWin.draw(*it = box3);
	}

This code actually makes it move. Ok by changing some values I got it to be more active. However I had wanted it to make multiple blocks, right now it only draws one block.

Edited by LeftyGuitar

Share this post


Link to post
Share on other sites

The code inside the for loop is supposed to iterate through the bodies.


No, that's what the loop does.

Look closer at the stuff inside the loop, particularly at the assignment you do inside the loop. It is probably not what you expect it to do.

Share this post


Link to post
Share on other sites

*it = box3


I want to know why you wrote that. Sometimes I can look at code and say "ok, I can see how they came up with that." Not this time. I'm totally stumped. Edited by Nypyren

Share this post


Link to post
Share on other sites

 

it* = box3;

 

I wrote that, because that is the only way it would draw the graphics on the screen. I'm sure there's a better way to do it, but it was a simple solution. I'm actually looking at other code, similar to what I wanna do and I can see what I was doing wrong, so I am working on fixing that.

Share this post


Link to post
Share on other sites

 

 

it* = box3;

 

I wrote that, because that is the only way it would draw the graphics on the screen. I'm sure there's a better way to do it, but it was a simple solution. I'm actually looking at other code, similar to what I wanna do and I can see what I was doing wrong, so I am working on fixing that.

 

What you're doing here is equivalent to  having a thousand customer records and you copy john doe's record over all of them.

Typical programmer steps would be to verify that each part of the code is working with your debugger. Is the container actually filled with the right things? Are you iterating over it correctly? Is the information pulled from the container actually being used correctly by your later code?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this