/** 开始拖动刚体*/ private void startDragBody(b2Body b, float x, float y) { if (b == null || b.GetType() != b2Body.b2_dynamicBody) { return; } if (_mj != null) { _world.DestroyJoint(_mj); } b2MouseJointDef jointDef = new b2MouseJointDef(); jointDef.bodyA = _world.GetGroundBody(); jointDef.bodyB = b; jointDef.target.Set(x, y); jointDef.maxForce = 1e6f; _mj = _world.CreateJoint(jointDef) as b2MouseJoint; }
public virtual bool MouseDown(b2Vec2 p) { m_mouseWorld = p; if (m_mouseJoint != null) { return(false); } // Make a small box. b2AABB aabb = new b2AABB(); b2Vec2 d = new b2Vec2(); d.Set(0.001f, 0.001f); aabb.LowerBound = p - d; aabb.UpperBound = p + d; // Query the world for overlapping shapes. QueryCallback callback = new QueryCallback(p); m_world.QueryAABB(callback, aabb); if (callback.m_fixture != null) { b2Body body = callback.m_fixture.Body; b2MouseJointDef md = new b2MouseJointDef(); md.BodyA = m_groundBody; md.BodyB = body; md.target = p; md.maxForce = 1000.0f * body.Mass; m_mouseJoint = (b2MouseJoint)m_world.CreateJoint(md); body.SetAwake(true); return(true); } return(false); }
public Wheel( b2World b2world, double x, double y, double width, double length, bool revolving, bool powered ) { this.revolving = revolving; this.powered = powered; this.Initialize = car => { /* * wheel object * * pars: * * car - car this wheel belongs to * x - horizontal position in meters relative to car's center * y - vertical position in meters relative to car's center * width - width in meters * length - length in meters * revolving - does this wheel revolve when steering? * powered - is this wheel powered? */ var position = new double[] { x, y }; //this.car=pars.car; //this.revolving=pars.revolving; //this.powered=pars.powered; //initialize body var def = new b2BodyDef(); def.type = b2Body.b2_dynamicBody; def.position = car.body.GetWorldPoint(new b2Vec2(position[0], position[1])); def.angle = car.body.GetAngle(); this.body = b2world.CreateBody(def); //initialize shape var fixdef = new b2FixtureDef(); fixdef.density = 1; fixdef.isSensor = true; //wheel does not participate in collision calculations: resulting complications are unnecessary var fixdef_shape = new b2PolygonShape(); fixdef.shape = fixdef_shape; fixdef_shape.SetAsBox(width / 2, length / 2); body.CreateFixture(fixdef); var jointdef = new b2RevoluteJointDef(); //create joint to connect wheel to body if (revolving) { jointdef.Initialize(car.body, body, body.GetWorldCenter()); jointdef.enableMotor = false; //we'll be controlling the wheel's angle manually } else { jointdef.Initialize(car.body, body, body.GetWorldCenter() //, new b2Vec2(1, 0) ); jointdef.enableLimit = true; //jointdef.lowerTranslation = 0; //jointdef.upperTranslation = 0; } b2world.CreateJoint(jointdef); #region setAngle this.setAngle = (angle) => { /* * angle - wheel angle relative to car, in degrees */ body.SetAngle(car.body.GetAngle() + angle.DegreesToRadians()); }; #endregion #region getLocalVelocity Func <double[]> getLocalVelocity = delegate { /*returns get velocity vector relative to car */ var res = car.body.GetLocalVector(car.body.GetLinearVelocityFromLocalPoint(new b2Vec2(position[0], position[1]))); return(new double[] { res.x, res.y }); }; #endregion #region getDirectionVector Func <double[]> getDirectionVector = delegate { /* * returns a world unit vector pointing in the direction this wheel is moving */ if (getLocalVelocity()[1] > 0) { return(vectors.rotate(new double[] { 0, 1 }, body.GetAngle())); } else { return(vectors.rotate(new double[] { 0, -1 }, body.GetAngle())); } }; #endregion #region getKillVelocityVector Func <double[]> getKillVelocityVector = delegate { /* * substracts sideways velocity from this wheel's velocity vector and returns the remaining front-facing velocity vector */ var velocity = body.GetLinearVelocity(); var sideways_axis = getDirectionVector(); var dotprod = vectors.dot(new[] { velocity.x, velocity.y }, sideways_axis); return(new double[] { sideways_axis[0] * dotprod, sideways_axis[1] * dotprod }); }; #endregion #region killSidewaysVelocity this.killSidewaysVelocity = delegate { /* * removes all sideways velocity from this wheels velocity */ var kv = getKillVelocityVector(); body.SetLinearVelocity(new b2Vec2(kv[0], kv[1])); }; #endregion }; }