class Craft(object): def __init__(self, windspeed, relative_scale, world): self.air = Air(b2Vec2(-windspeed, 0.0)) self.water = Water(b2Vec2(0.0, 0.0)) self.world = world gap = 10.0 h_length = 1.0 h_wchord = 0.5 h_wspan = 2.0 h_schord = 0.25 h_sspan = 0.75 a_length = h_length * relative_scale a_wchord = h_wchord * relative_scale a_wspan = h_wspan * relative_scale a_schord = h_schord * relative_scale a_sspan = h_sspan * relative_scale self.airfoil = Glider(x=0.0, y=gap * 0.5, anchor_point=0.12, polarity=1.0, body_length=a_length, wing_chord=a_wchord, wing_span=a_wspan, stabilizer_chord=a_schord, stabilizer_span=a_sspan, fluid=self.air, box2dWorld=world, density=50.0) self.hydrofoil = Glider(x=0.0, y=gap * -0.5, anchor_point=0.12, polarity=-1.0, body_length=h_length, wing_chord=h_wchord, wing_span=h_wspan, stabilizer_chord=h_schord, stabilizer_span=h_sspan, fluid=self.water, box2dWorld=world, density=300.0) self.add_anchor_joint() def windspeed(self): return self.air.velocity.Length() def move_anchor(self, glider, amount): glider.move_anchor(amount) if hasattr(self, "anchorJoint"): self.world.DestroyJoint(self.anchorJoint) self.add_anchor_joint() def add_anchor_joint(self): anchorJoint = b2DistanceJointDef() anchor1 = self.airfoil.get_anchor_point() anchor2 = self.hydrofoil.get_anchor_point() anchorJoint.Initialize(self.airfoil.body, self.hydrofoil.body, anchor1, anchor2) anchorJoint.collideConnected = True self.anchorJoint = self.world.CreateJoint(anchorJoint).getAsType() def get_center(self): return (self.airfoil.body.GetPosition() + self.hydrofoil.body.GetPosition()) * 0.5 def step(self, damper=0.0): self.airfoil.step(damper) self.hydrofoil.step(damper) def draw(self, ctx): self.airfoil.draw(ctx) self.hydrofoil.draw(ctx) def get_force_vectors(self): forces = [] forces += self.airfoil.get_force_vectors() forces += self.hydrofoil.get_force_vectors() return forces def clear_force_vectors(self): self.airfoil.clear_force_vectors() self.hydrofoil.clear_force_vectors() def velocity(self): return (self.hydrofoil.velocity() + self.airfoil.velocity()) / 2.0
class Craft(object): def __init__(self, windspeed, relative_scale, world): self.air = Air(b2Vec2(-windspeed, 0.0)) self.water = Water(b2Vec2(0.0,0.0)) self.world = world gap = 10.0 h_length = 1.0 h_wchord = 0.5 h_wspan = 2.0 h_schord = 0.25 h_sspan = 0.75 a_length = h_length * relative_scale a_wchord = h_wchord * relative_scale a_wspan = h_wspan * relative_scale a_schord = h_schord * relative_scale a_sspan = h_sspan * relative_scale self.airfoil = Glider (x=0.0, y=gap*0.5, anchor_point=0.12, polarity=1.0, body_length=a_length, wing_chord=a_wchord, wing_span=a_wspan, stabilizer_chord=a_schord, stabilizer_span=a_sspan, fluid=self.air, box2dWorld=world, density=50.0) self.hydrofoil = Glider (x=0.0, y=gap*-0.5, anchor_point=0.12, polarity=-1.0, body_length=h_length, wing_chord=h_wchord, wing_span=h_wspan, stabilizer_chord=h_schord, stabilizer_span=h_sspan, fluid=self.water, box2dWorld=world, density=300.0) self.add_anchor_joint() def windspeed(self): return self.air.velocity.Length() def move_anchor(self, glider, amount): glider.move_anchor(amount) if hasattr(self, "anchorJoint"): self.world.DestroyJoint(self.anchorJoint) self.add_anchor_joint() def add_anchor_joint(self): anchorJoint = b2DistanceJointDef() anchor1 = self.airfoil.get_anchor_point() anchor2 = self.hydrofoil.get_anchor_point() anchorJoint.Initialize(self.airfoil.body, self.hydrofoil.body, anchor1, anchor2); anchorJoint.collideConnected = True self.anchorJoint = self.world.CreateJoint(anchorJoint).getAsType() def get_center(self): return (self.airfoil.body.GetPosition() + self.hydrofoil.body.GetPosition()) * 0.5 def step(self, damper=0.0): self.airfoil.step(damper) self.hydrofoil.step(damper) def draw(self, ctx): self.airfoil.draw(ctx) self.hydrofoil.draw(ctx) def get_force_vectors(self): forces = [] forces += self.airfoil.get_force_vectors() forces += self.hydrofoil.get_force_vectors() return forces def clear_force_vectors(self): self.airfoil.clear_force_vectors() self.hydrofoil.clear_force_vectors() def velocity(self): return (self.hydrofoil.velocity() + self.airfoil.velocity()) / 2.0
class Kite(object): def __init__(self, x, y, anchor_point, polarity, body_length, wing_chord, wing_span, stabilizer_chord, stabilizer_span, fluid, box2dWorld): self.glider = Glider(x, y, anchor_point, polarity, body_length, wing_chord, wing_span, stabilizer_chord, stabilizer_span, fluid, box2dWorld) self.original_x = x # Make an anchor anchorBodyDef = b2BodyDef() anchorBodyDef.position.Set(x, 0.0) anchor = box2dWorld.CreateBody(anchorBodyDef) shapeDef = b2PolygonDef() shapeDef.SetAsBox(1.0, 1.0) shapeDef.density = 1000 # kg/m^3, from wikipedia for styrofoam density anchor.CreateShape(shapeDef) anchor.SetMassFromShapes() self.anchor = anchor self.world = box2dWorld # Create the anchor joint self.move_anchor(0.0) def MakeTestKite(fluid, world): return Kite(x=2.0, y=20.0, anchor_point=0.12, polarity=1, body_length=6.0, wing_chord=1, wing_span=4.0, stabilizer_chord=0.5, stabilizer_span=1.5, fluid=fluid, box2dWorld=world) def MakeGiantKite(fluid, world): return Kite(x=2.0, y=20.0, anchor_point=0.12, polarity=1, body_length=8.0, wing_chord=2, wing_span=12.0, stabilizer_chord=1, stabilizer_span=8.0, fluid=fluid, box2dWorld=world) MakeTestKite = staticmethod(MakeTestKite) MakeGiantKite = staticmethod(MakeGiantKite) def get_force_vectors(self): return self.glider.get_force_vectors() def clear_force_vectors(self): self.glider.clear_force_vectors() def move_anchor(self, amount): anchor_point = self.glider.move_anchor(amount) if hasattr(self, "anchorJoint"): self.world.DestroyJoint(self.anchorJoint) self.add_anchor_joint(anchor_point) def add_anchor_joint(self, anchor_point): # Make a pully joint connecting the mount to the anchor anchorJoint = b2PulleyJointDef() anchor2 = self.anchor.GetWorldCenter() groundAnchor1 = b2Vec2(self.original_x - 0.2, 0.2) groundAnchor2 = b2Vec2(self.original_x - 0.1, 0.2) ratio = 1.0 anchorJoint.Initialize(self.glider.body, self.anchor, groundAnchor1, groundAnchor2, anchor_point, anchor2, ratio) anchorJoint.maxLength2 = 10.0 anchorJoint.collideConnected = False self.anchorJoint = self.world.CreateJoint(anchorJoint).getAsType() def step(self, damper=0.0): self.glider.step(damper) def draw(self, ctx): self.glider.draw(ctx)
class Kite(object): def __init__(self, x, y, anchor_point, polarity, body_length, wing_chord, wing_span, stabilizer_chord, stabilizer_span, fluid, box2dWorld): self.glider = Glider(x, y, anchor_point, polarity, body_length, wing_chord, wing_span, stabilizer_chord, stabilizer_span, fluid, box2dWorld) self.original_x = x # Make an anchor anchorBodyDef = b2BodyDef() anchorBodyDef.position.Set(x, 0.0) anchor = box2dWorld.CreateBody(anchorBodyDef) shapeDef = b2PolygonDef() shapeDef.SetAsBox(1.0, 1.0) shapeDef.density = 1000 # kg/m^3, from wikipedia for styrofoam density anchor.CreateShape(shapeDef) anchor.SetMassFromShapes() self.anchor = anchor self.world = box2dWorld # Create the anchor joint self.move_anchor(0.0) def MakeTestKite(fluid, world): return Kite(x=2.0, y=20.0, anchor_point=0.12, polarity=1, body_length=6.0, wing_chord=1, wing_span=4.0, stabilizer_chord=0.5, stabilizer_span=1.5, fluid=fluid, box2dWorld=world) def MakeGiantKite(fluid, world): return Kite(x=2.0, y=20.0, anchor_point=0.12, polarity=1, body_length=8.0, wing_chord=2, wing_span=12.0, stabilizer_chord=1, stabilizer_span=8.0, fluid=fluid, box2dWorld=world) MakeTestKite = staticmethod(MakeTestKite) MakeGiantKite = staticmethod(MakeGiantKite) def get_force_vectors(self): return self.glider.get_force_vectors() def clear_force_vectors(self): self.glider.clear_force_vectors() def move_anchor(self, amount): anchor_point = self.glider.move_anchor(amount) if hasattr(self, "anchorJoint"): self.world.DestroyJoint(self.anchorJoint) self.add_anchor_joint(anchor_point) def add_anchor_joint(self, anchor_point): # Make a pully joint connecting the mount to the anchor anchorJoint = b2PulleyJointDef() anchor2 = self.anchor.GetWorldCenter() groundAnchor1 = b2Vec2(self.original_x - 0.2, 0.2) groundAnchor2 = b2Vec2(self.original_x - 0.1, 0.2) ratio = 1.0 anchorJoint.Initialize(self.glider.body, self.anchor, groundAnchor1, groundAnchor2, anchor_point, anchor2, ratio); anchorJoint.maxLength2 = 10.0 anchorJoint.collideConnected = False self.anchorJoint = self.world.CreateJoint(anchorJoint).getAsType() def step(self, damper=0.0): self.glider.step(damper) def draw(self, ctx): self.glider.draw(ctx)