Asteroids Remake

Hi all, I need a bit help here. I’m making an Asteroids clone game, using Box2d. I’m in trouble with the spaceship movements. I use :

ship:applyLinearImpulse( fx, fy, ship.x, ship.y )

in Runtime when user touch a button, the force is applied along the correct ship axe with some trigonometry calc.

The ship surfs very well so like the original of Atari.

Problems come when I try to set a max speed for the ship, I’ve tried several paths but I crash in strange behaviours, and none of them works. The true is that I have no idea on how to make this.

I know that for Newton laws also a minimum continous force applied to an object in space accelerates the object infinitely up to the speed of light, if we do not consider Einstein’s relative theory, but this is just a game so…but maybe could be a little hard to set this max speed inside a physic engine like Box2d?

I know that I could make the game without Box2d, but I have created 2 little games with Corona without it, I would use it for this.

Sorry for my English.

Add an enterFrame event listener. Inside the listener, set the object’s speed to math.min(object’s current speed, max speed). You can get/set the speed using object:get/setLinearVelocity()

If you want the object’s speed to be linear, rather than accelerating to a max speed, you can also choose not to use applyLinearImpulse() at all, and just use object:setLinearVelocity() instead

I tried to develop your advice, but the spacecraft moves lamely, probably I did not understand exactly what you mean. Anyway I finally found a method that behaves better, using the damping and setting a linear speed for the spacecraft when the user releases the key propulsion.

But now there is another anomaly. At maximum speed  the ship rotates right perfectly whipping sharply as in the original game, instead, if it wheel to the left takes a long time, as if there was a constant force of rotation to the right which is opposed. I just can not explain why.

How are you rotating the ship? Are you using object.isFixedRotation = true and rotating it by directly setting ship.rotation (without physics), or can the ship rotate freely and you’re applying angular velocity to it (with physics)?

I’m trying to rotate the ship in 3 ways:

[lua]

local function rotateLeft(ship,engine)

    1 --ship.rotation=ship.rotation(-<value>)

    2 – ship:rotate(-<value>)

    3 --ship:applyTorque(-<value> )

    4 --ship.angularDamping=ship.angularVelocity/ship.mass 

       –  ship:applyAngularImpulse( -<value> )

end

[/lua]

But none of them works. Rotate right works, but rotate left is slow and cause random crazy spin moves.

I would use angularImpulse because is more realistic effect.

Did you remember to set ship.isFixedRotation = true before using ship.rotation = x or ship:rotate() ?

I’ve got your tip and I tried to insert ship.isFixedRotation = true. The ship now is more stable, but sometime the ship goes crazy again. I’ll try to post a video maybe.

I suspect that the problem may be due to the fact that the direction of propulsion is calculated using the line bisecting the anchors of the two parts of the ship. The engine and the cabin. The motor is connected to the cab by two joint, and I see that when I rotate for a while the vessel, the joint appear elastic, and the engine looks distorted. Also after a while that the ship is governed in every possible maneuver, the ship goes crazy beginning to rotate and move forward speed as governed by uncontrollable forces resulting from all the previous maneuvers.

Oh, your ship is actually assembled from several parts and with joints? I assumed it was all one piece, but being a complex object, it’s difficult to answer the question without seeing a screenshot of the ship and its construction.

Maybe I have resolved rewriting various code parts. Thank you for the help.

I realized that by limiting the maximum speed the ship has lost the ability to go into a spin with the engine running. My ship get now perfect circle instead of spiral. But as they did?

https://www.atari.com/arcade#!/arcade/asteroids/play

Add an enterFrame event listener. Inside the listener, set the object’s speed to math.min(object’s current speed, max speed). You can get/set the speed using object:get/setLinearVelocity()

If you want the object’s speed to be linear, rather than accelerating to a max speed, you can also choose not to use applyLinearImpulse() at all, and just use object:setLinearVelocity() instead

I tried to develop your advice, but the spacecraft moves lamely, probably I did not understand exactly what you mean. Anyway I finally found a method that behaves better, using the damping and setting a linear speed for the spacecraft when the user releases the key propulsion.

But now there is another anomaly. At maximum speed  the ship rotates right perfectly whipping sharply as in the original game, instead, if it wheel to the left takes a long time, as if there was a constant force of rotation to the right which is opposed. I just can not explain why.

How are you rotating the ship? Are you using object.isFixedRotation = true and rotating it by directly setting ship.rotation (without physics), or can the ship rotate freely and you’re applying angular velocity to it (with physics)?

I’m trying to rotate the ship in 3 ways:

[lua]

local function rotateLeft(ship,engine)

    1 --ship.rotation=ship.rotation(-<value>)

    2 – ship:rotate(-<value>)

    3 --ship:applyTorque(-<value> )

    4 --ship.angularDamping=ship.angularVelocity/ship.mass 

       –  ship:applyAngularImpulse( -<value> )

end

[/lua]

But none of them works. Rotate right works, but rotate left is slow and cause random crazy spin moves.

I would use angularImpulse because is more realistic effect.

Did you remember to set ship.isFixedRotation = true before using ship.rotation = x or ship:rotate() ?

I’ve got your tip and I tried to insert ship.isFixedRotation = true. The ship now is more stable, but sometime the ship goes crazy again. I’ll try to post a video maybe.

I suspect that the problem may be due to the fact that the direction of propulsion is calculated using the line bisecting the anchors of the two parts of the ship. The engine and the cabin. The motor is connected to the cab by two joint, and I see that when I rotate for a while the vessel, the joint appear elastic, and the engine looks distorted. Also after a while that the ship is governed in every possible maneuver, the ship goes crazy beginning to rotate and move forward speed as governed by uncontrollable forces resulting from all the previous maneuvers.

Oh, your ship is actually assembled from several parts and with joints? I assumed it was all one piece, but being a complex object, it’s difficult to answer the question without seeing a screenshot of the ship and its construction.

Maybe I have resolved rewriting various code parts. Thank you for the help.

I realized that by limiting the maximum speed the ship has lost the ability to go into a spin with the engine running. My ship get now perfect circle instead of spiral. But as they did?

https://www.atari.com/arcade#!/arcade/asteroids/play