Flying object (spaceship like)

Hi there !

I'm designing a game and I got a question for you.

I'd like to have my character flying in the air without feeling gravity, it goes up or down only if I hit the corresponding buttons.

I still want to simulate physics in my game so Box2D seems to be a good option as usual. The thing is I don't know what to do with my flying character. Is static body the good way to do it ? Or should I just move a sensor as I move the sprite without any body ?
I'm not sure since I want any collision to kill the character.

Thanks a lot ! 😀
Sensors are used for collision data but wont collide with any physical bodies,
So what you would want is a dynamic type body with a fixture set as a sensor

i dunno what language you are using so i will just write in as3 since its easy to understand
Code:
var body:b2Body;
var fix:b2FixtureDef = new b2FixtureDef();
var poly:b2PolygonShape = new b2PolygonShape();
var def:b2BodyDef = new b2BodyDef();
         
poly.SetAsBox(0.5, 0.5);
def.type = b2Body.b2_dynamicBody;
fix.shape = poly;
fix.density = 0.5;
fix.friction = 0.5;
fix.restitution = 0.1;
fix.isSensor = true;//<- Sensor
def.position = new b2Vec2(0, 0);
body = world.CreateBody(def);
body.CreateFixture(fix);


and then to simulate no gravity
Code:
var worldGravity = new b2Vec2(0.0, 10.0);
//negate gravity
body.ApplyForce(new b2Vec2( 0, -worldGravity.y * body.GetMass()), body.GetWorldCenter());


hope it helps 😛
That's right, I forgot about dynamic bodies… [edit : Don't know why I said that… Of course I wouldn't forget about them]

I'll have to think if its really what I want though.
Is that adapted for being moved "precily" ? I mean not by forces.
I think acceleration for up and down motion does not suit such a game in which you have to be fast and precise in moving.

Thanks a lot ! 🙂
Well i mean if you don't want it to be affected by forces

you could just make it kinematic and change its velocity manually
Code:
//OnUpdate
var inputVel:b2Vec2 = new b2Vec2();

if (keydown_Up)
    inputVel.y = -1;
if (keydown_Down)
    inputVel.y = 1;

body.SetLinearVelocity(inputVel);

sorry for the late reply :[
Actually I was mistaken and though that's what you suggested me…

Thanks. I think that is the better option for me.

A kinematic body with a sensor for collisions should work. 🙂

Original thread: https://box2d.org/forum/viewtopic.php?t=9050