Hello. I'm taking my first steps at using Box2D Physics engine. I've got an example working with OpenGL, here is all the code related to Box2D:
This is a film that shows the example in action http://www.youtube.com/watch?v=osmRppk9lFY
I'd like to make the red box angle always corresponding to direction (just like homing missile, blaster shot from star wars movie etc.) is there any simple way to do that with Box2D, without having to calculate rotation with atan from the position of the blaster from the two last frames? Do you have any other conception? I'm looking forward for some ideas about solving this problem. Thanks in advance.
Code:
from Box2D import *
######init:
worldAABB=b2AABB()
worldAABB.lowerBound = (-100, -100)
worldAABB.upperBound = ( 100, 100)
gravity = b2Vec2(0, 0)
self.world = b2World(worldAABB, gravity, True)
### Lightsaber
body = b2BodyDef()
body.position.Set(0.0, 0.0)
geom = b2PolygonDef()
geom.SetAsBox(0.2, 1.5)
geom.friction=0.3
geom.density=10000.0
self.miecz=self.world.CreateBody(body)
self.miecz.CreateShape(geom)
self.miecz.SetMassFromShapes()
self.miecz.SetAngularVelocity(-2.0)
### Blaster
body = b2BodyDef()
body.position.Set(4.0, 4.0)
geom = b2PolygonDef()
geom.SetAsBox(0.1, 0.5)
geom.friction=0.3
geom.density=1.0
self.blaster=self.world.CreateBody(body)
self.blaster.CreateShape(geom)
self.blaster.SetMassFromShapes()
#self.blaster.fixedRotation=True
self.blaster.SetXForm(self.blaster.GetPosition(),math.degrees(45.0))
self.blaster.SetLinearVelocity((-2,-2))
##### the last lines of draw function:
timeStep = 1.0 / 40
vel_iters, pos_iters = 10, 8
self.world.Step(timeStep, vel_iters, pos_iters)
######init:
worldAABB=b2AABB()
worldAABB.lowerBound = (-100, -100)
worldAABB.upperBound = ( 100, 100)
gravity = b2Vec2(0, 0)
self.world = b2World(worldAABB, gravity, True)
### Lightsaber
body = b2BodyDef()
body.position.Set(0.0, 0.0)
geom = b2PolygonDef()
geom.SetAsBox(0.2, 1.5)
geom.friction=0.3
geom.density=10000.0
self.miecz=self.world.CreateBody(body)
self.miecz.CreateShape(geom)
self.miecz.SetMassFromShapes()
self.miecz.SetAngularVelocity(-2.0)
### Blaster
body = b2BodyDef()
body.position.Set(4.0, 4.0)
geom = b2PolygonDef()
geom.SetAsBox(0.1, 0.5)
geom.friction=0.3
geom.density=1.0
self.blaster=self.world.CreateBody(body)
self.blaster.CreateShape(geom)
self.blaster.SetMassFromShapes()
#self.blaster.fixedRotation=True
self.blaster.SetXForm(self.blaster.GetPosition(),math.degrees(45.0))
self.blaster.SetLinearVelocity((-2,-2))
##### the last lines of draw function:
timeStep = 1.0 / 40
vel_iters, pos_iters = 10, 8
self.world.Step(timeStep, vel_iters, pos_iters)
This is a film that shows the example in action http://www.youtube.com/watch?v=osmRppk9lFY
I'd like to make the red box angle always corresponding to direction (just like homing missile, blaster shot from star wars movie etc.) is there any simple way to do that with Box2D, without having to calculate rotation with atan from the position of the blaster from the two last frames? Do you have any other conception? I'm looking forward for some ideas about solving this problem. Thanks in advance.