Box2d - Pointentity and add local force
BlitzMax Forums/Brucey's Modules/Box2d - Pointentity and add local force
| ||
Hi, I want to make a body (shot) move to to a specified point. So I need the body to' look' at the point and then add some local force to make him move 'forward'. But I can't figure out which of my functions fails. Function ApplyLocalForce(body:b2Body, force:Float) Local vec:b2Vec2 = body.GetWorldVector(New b2Vec2.Create(0, force)) Local pos:b2Vec2 = body.GetWorldPoint(New b2Vec2.Create(0, 0)) body.ApplyImpulse(vec, pos) End Function Function PointtoVector(entity:b2Body, vec:b2Vec2) Local pos:b2Vec2 = entity.GetPosition() Local angle:Float = GetAngle(pos.X(), pos.Y(), vec.X(), vec.Y()) entity.SetXForm(entity.GetPosition(), angle * -1) End Function |
| ||
Impulse require a target vector and don't rely on the object angle. I don't have BM on this machine but try to work with something like this : Function ApplyLocalForce(body:b2Body, force:Float) Local VXP:float,VYP:float Local angle:float = body.GetAngle() VXP = Sin(angle) * force VYP = Cos(angle) * force body.ApplyImpulse(Vec2(VXP, VYP), body.GetPosition()) End Function Vec2(0, 0) is a good shortcut for New b2Vec2.Create(0, 0) ;-) body.GetPosition() will always return a World Vector. Don't forget to add continuous collision detection (CCD) on your body in order to prevent "Tunneling". |
| ||
Thanks a lot, will give it a try tomorrow. |