Box2D Forums

It is currently Thu May 23, 2013 9:53 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Tue Nov 15, 2011 9:07 am 
Offline

Joined: Tue Nov 15, 2011 8:17 am
Posts: 3
Hi everyone,

I am making my first box2d flash as3 game just like pong, but have faced a very weird problem,
first of all I create walls left right top and bottom

Code:
function CreateBound(){
   
   var bodyDef:b2BodyDef = new b2BodyDef();
   bodyDef.position.Set(0 / m_physScale, 0 / m_physScale);
   //left
   var leftSide:b2PolygonShape = new b2PolygonShape();
   leftSide.SetAsEdge(new b2Vec2(0, 0/ m_physScale),new b2Vec2(0/ m_physScale, 320 / m_physScale));
   var leftFixt:b2FixtureDef = new b2FixtureDef();
   leftFixt.shape = leftSide;
   //right
   var rightSide:b2PolygonShape = new b2PolygonShape();
   rightSide.SetAsEdge(new b2Vec2(480/ m_physScale, 0/ m_physScale),new b2Vec2(480/ m_physScale, 320 / m_physScale));
   var rightFixt:b2FixtureDef = new b2FixtureDef();
   rightFixt.shape = rightSide;
   //top
   var topSide:b2PolygonShape = new b2PolygonShape();
   topSide.SetAsEdge(new b2Vec2(0/ m_physScale, 0/ m_physScale),new b2Vec2(480/ m_physScale, 0 / m_physScale));
   var topFixt:b2FixtureDef = new b2FixtureDef();
   topFixt.shape = topSide;
   //bottom
   var bottomSide:b2PolygonShape = new b2PolygonShape();
   bottomSide.SetAsEdge(new b2Vec2(0/ m_physScale, 320/ m_physScale),new b2Vec2(480/ m_physScale, 320 / m_physScale));
   var bottomFixt:b2FixtureDef = new b2FixtureDef();
   bottomFixt.shape = bottomSide;
   
   var world_body:b2Body = world.CreateBody(bodyDef);
   world_body.CreateFixture(leftFixt);
   world_body.CreateFixture(rightFixt);
   world_body.CreateFixture(topFixt);
   world_body.CreateFixture(bottomFixt);
   
   
}


then create the 'ball'

Code:
var bodyDef2:b2BodyDef = new b2BodyDef();
 bodyDef2.position.Set(249 / m_physScale, 100 / m_physScale);
 bodyDef2.type = b2Body.b2_dynamicBody;
 bodyDef2.linearVelocity.Set(50,50);
 bodyDef2.inertiaScale = 2;
 //bodyDef2.angularVelocity = 1;
 body = world.CreateBody(bodyDef2);
 var fd:b2FixtureDef = new b2FixtureDef();
 fd.shape = mycircle;
 fd.restitution = 1;
 fd.density = 1;
 body.CreateFixture(fd);


everything works fine in the beginning....after the 'ball' rebounds a few times with the 'walls', the 'ball' keep rebounding with the top and bottom 'wall' and collision and same point everytime, that's mean the 'ball' keep moving in the same track, up and down, up and down, and never move the collide left/ right wall again........What 's the problem??
Thanks for any kindly help!!


Top
 Profile  
 
PostPosted: Tue Nov 15, 2011 6:52 pm 
Offline

Joined: Sun Oct 25, 2009 3:28 am
Posts: 242
Try setting the ball's friction to 0 (zero). The rotation of the ball may be causing this glitch.


Top
 Profile  
 
PostPosted: Tue Nov 15, 2011 10:01 pm 
Offline

Joined: Tue Nov 15, 2011 8:17 am
Posts: 3
@ jayther

Thanks for you reply, I set the "fd.friction = 0;"
seems the problem become less frequent.

Another question(sorry for tons of question....), anyway to make the ball move faster?
It moved as before even I set "bodyDef2.linearVelocity.Set(1500,1500);"

Thanks for any kindly help!!!!


Top
 Profile  
 
PostPosted: Wed Nov 16, 2011 1:41 am 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1517
Location: Tokyo
I made a breakout type game a while ago and I got this problem too. Actually it was surprisingly difficult to handle.

I found that to fix it completely I had to look at the velocity of the ball when the collision started, and then set it back again after the collision ended to preserve the speed. Also, this had to be done for both the perpendicular speed (reverse this to make a perfectly elastic bounce) and the tangential speed (keep it the same). This doesn't work if the ball can hit two things at the same time but in my case it was ok.

There were also problems when the ball hit the corner of a block - in this case I looked at the collision normal and used the largest of the x/y dimensions to pretend that it had actually hit a flat wall. For example, if the collision normal was (0.53, 0.85) I would pretend that it was (0,1).

Even with all this there are still problems occasionally but they are rare. If it seems difficult remember that it's not realistic physics, and you always get issues when you mix non-realism with Box2D.

About the maximum velocity for a body, you may need to scale your physics world down a little. See the first question on this page for an explanation:
http://www.iforce2d.net/b2dtut/gotchas


Top
 Profile  
 
PostPosted: Wed Nov 16, 2011 1:44 am 
Offline

Joined: Sun Oct 25, 2009 3:28 am
Posts: 242
You will need to use SetLinearVelocity on the body instance (the "body" variable, not "bodyDef"), or apply a force or impulse using ApplyForce or ApplyImpulse (ApplyForce, depending on the amount of force and the fact you're using it in more than one frame, will have a gradual increase in speed, while ApplyImpulse will take effect immediately).

A tip: definitions (like the body definitions) are only used for creating the stuff you need in the beginning (which is why nothing happens when you edit the properties in a body definition). If you want to alter the behavior after creation, you will need to use the functions for the actual instance to change their behaviors (i.e. b2BodyDef is the body definition and b2Body is the actual instance of the body).


Top
 Profile  
 
PostPosted: Wed Nov 16, 2011 8:32 pm 
Offline

Joined: Tue Nov 15, 2011 8:17 am
Posts: 3
jayther wrote:
You will need to use SetLinearVelocity on the body instance (the "body" variable, not "bodyDef"), or apply a force or impulse using ApplyForce or ApplyImpulse (ApplyForce, depending on the amount of force and the fact you're using it in more than one frame, will have a gradual increase in speed, while ApplyImpulse will take effect immediately).

A tip: definitions (like the body definitions) are only used for creating the stuff you need in the beginning (which is why nothing happens when you edit the properties in a body definition). If you want to alter the behavior after creation, you will need to use the functions for the actual instance to change their behaviors (i.e. b2BodyDef is the body definition and b2Body is the actual instance of the body).


I have made a mouse event to apply force to the body,

Code:
stage.addEventListener(MouseEvent.CLICK,applyforces);
}
function applyforces(e:MouseEvent):void {
   body.ApplyForce(new b2Vec2(1500,-1500), body.GetWorldCenter());      
}


I click and click repeatedly and attempt to increase to a crazy speed, but still fail, seems it has a maximum speed to the ball...?
or I just misunderstand the usage of applyForce?(how the function calculate the force??)

Thanks for helping~


Top
 Profile  
 
PostPosted: Wed Nov 16, 2011 10:14 pm 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1517
Location: Tokyo
Bodies have a maximum speed set by default.

See my answer a few posts above :)


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: Bing [Bot] and 5 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® Forum Software © phpBB Group