Hi!
I am leaning box2d ver 2.1a for as3
How to simulate the movement of reflection from the wall ?
Some code definitions:
My questions are:
1) Why the ball don't reflect from a wall ?
2) How to organize the rebound that the angle of incidence was equal to the angle of reflection?
3) How to give the ball movement just knowing the angle?
applayImpulse requires the coordinates for the vector, which is inconvenient
Who can help me?
I am leaning box2d ver 2.1a for as3
How to simulate the movement of reflection from the wall ?
Some code definitions:
Code:
//---WIDTH 640 HEIGHT 480
//---ball definition-----------------
var ballFd:b2FixtureDef = new b2FixtureDef();
var ballBd:b2BodyDef = new b2BodyDef();
ballBd.type = b2Body.b2_dynamicBody;//b2Body.b2_kinematicBody;
ballFd.density = 1.0;
ballFd.restitution = 1;
ballFd.shape = new b2CircleShape(5);
ballbody = world.CreateBody(ballBd);
ballbody.SetPosition(new b2Vec2(WIDTH/2,HEIGHT/5));
//----- left wall define-------------
var wallFd:b2FixtureDef = new b2FixtureDef();
var wallSd:b2PolygonShape = new b2PolygonShape();
var wallBd:b2BodyDef = new b2BodyDef();
wallSd.SetAsBox(10,WIDTH);
wallBd.position.Set(0, 0);
wallBd.type = b2Body.b2_staticBody;
wallFd.density = 1.0;
wallFd.restitution = 0;
wallFd.shape = wallSd;
wallleft = world.CreateBody(wallBd);
wallleft.SetPosition(new b2Vec2(0,0));
addEventListener(MouseEvent.CLICK, onClick);
private function onClick(e:Event):void
{
//----ball moves to left wall----
ballbody.ApplyImpulse(new b2Vec2(-100,400), ballbody.GetPosition());
}
//---ball definition-----------------
var ballFd:b2FixtureDef = new b2FixtureDef();
var ballBd:b2BodyDef = new b2BodyDef();
ballBd.type = b2Body.b2_dynamicBody;//b2Body.b2_kinematicBody;
ballFd.density = 1.0;
ballFd.restitution = 1;
ballFd.shape = new b2CircleShape(5);
ballbody = world.CreateBody(ballBd);
ballbody.SetPosition(new b2Vec2(WIDTH/2,HEIGHT/5));
//----- left wall define-------------
var wallFd:b2FixtureDef = new b2FixtureDef();
var wallSd:b2PolygonShape = new b2PolygonShape();
var wallBd:b2BodyDef = new b2BodyDef();
wallSd.SetAsBox(10,WIDTH);
wallBd.position.Set(0, 0);
wallBd.type = b2Body.b2_staticBody;
wallFd.density = 1.0;
wallFd.restitution = 0;
wallFd.shape = wallSd;
wallleft = world.CreateBody(wallBd);
wallleft.SetPosition(new b2Vec2(0,0));
addEventListener(MouseEvent.CLICK, onClick);
private function onClick(e:Event):void
{
//----ball moves to left wall----
ballbody.ApplyImpulse(new b2Vec2(-100,400), ballbody.GetPosition());
}
My questions are:
1) Why the ball don't reflect from a wall ?
2) How to organize the rebound that the angle of incidence was equal to the angle of reflection?
3) How to give the ball movement just knowing the angle?
applayImpulse requires the coordinates for the vector, which is inconvenient
Who can help me?