0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

plunkerで liquidfun その12

Posted at

概要

plunkerで liquidfunやってみた。
キーイベントのサンプル見つけたので、やってみた。

写真

image.png

サンプルコード


function TestPinball() {
  var bd = new b2BodyDef;
  var ground = world.CreateBody(bd);
  var loop = new b2ChainShape;
  loop.vertices.push(new b2Vec2(0.0, -2.0));
  loop.vertices.push(new b2Vec2(8.0, 6.0));
  loop.vertices.push(new b2Vec2(8.0, 20.0));
  loop.vertices.push(new b2Vec2(-8.0, 20.0));
  loop.vertices.push(new b2Vec2(-8.0, 6.0));
  loop.CreateLoop();
  var fd = new b2FixtureDef;
  fd.shape = loop;
  fd.density = 0.0;
  ground.CreateFixtureFromDef(fd);
  var p1 = new b2Vec2(-2.0, 0.0);
  var p2 = new b2Vec2(2.0, 0.0);
  bd = new b2BodyDef;
  bd.type = b2_dynamicBody;
  bd.position = p1;
  var leftFlipper = world.CreateBody(bd);
  bd.position = p2;
  var rightFlipper = world.CreateBody(bd);
  var box = new b2PolygonShape;
  box.SetAsBoxXY(1.75, 0.1);
  fd = new b2FixtureDef;
  fd.shape = box;
  fd.density = 1.0;
  leftFlipper.CreateFixtureFromDef(fd);
  box = new b2PolygonShape;
  box.SetAsBoxXY(1.75, 0.1);
  fd = new b2FixtureDef;
  fd.shape = box;
  fd.density = 1.0;
  rightFlipper.CreateFixtureFromDef(fd);
  var jd = new b2RevoluteJointDef;
  jd.bodyA = ground;
  jd.enableMotor = true;
  jd.maxMotorTorque = 1000.0;
  jd.enableLimit = true;
  jd.motorSpeed = 0.0;
  jd.localAnchorA = p1;
  jd.bodyB = leftFlipper;
  jd.lowerAngle = -30.0 * Math.PI / 180.0;
  jd.upperAngle = 5.0 * Math.PI / 180.0;
  this.leftJoint = world.CreateJoint(jd);
  jd.motorSpeed = 0.0;
  jd.localAnchorA = p2;
  jd.bodyB = rightFlipper;
  jd.lowerAngle = -5.0 * Math.PI / 180.0;
  jd.upperAngle = 30.0 * Math.PI / 180.0;
  this.rightJoint = world.CreateJoint(jd);
  var bd = new b2BodyDef;
  bd.position.Set(6.0, 15.0);
  bd.type = b2_dynamicBody;
  bd.bullet = true;
  this.ball = world.CreateBody(bd);
  var shape = new b2CircleShape;
  shape.radius = 0.2;
  fd = new b2FixtureDef;
  fd.shape = shape;
  fd.density = 1.0;
  this.ball.CreateFixtureFromDef(fd);
  this.button = false;
}
TestPinball.prototype.Keyboard = function(key) {
  switch(key) 
  {
  case 'A':
  case 'a':
    this.button = true;
  break;
  }
};
TestPinball.prototype.KeyboardUp = function(key) {
  switch(key) 
  {
  case 'A':
  case 'a':
    this.button = false;
  break;
  }
};
TestPinball.prototype.Step = function() {
  if (this.button) 
  {
    this.leftJoint.SetMotorSpeed(20);
    this.rightJoint.SetMotorSpeed(-20);
  } 
  else 
  {
    this.leftJoint.SetMotorSpeed(-10);
    this.rightJoint.SetMotorSpeed(10);
  }
  Step();
};


成果物

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?