Realistic space flight

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
TheMeq
Citizen
Posts: 56
Joined: Fri Sep 02, 2011 9:56 pm
Location: Nottingham, UK

Realistic space flight

Post by TheMeq »

Hello all, im super stumped with this one.

I have my ship. I can apply acceleration to it, and I can rotate it and it will head in the direction the ship is rotated. The issue is, the ship turns instantly, instead of gliding in a sense.

Im stumped on how I can achieve this. Any ideas?
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: Realistic space flight

Post by Beelz »

Instead of forcing the ship to go whichever direction it's pointed you should have it follow whatever velocity it has gathered regardless of direction.

Try this in your update function, adjust accordingly:

Code: Select all

function love.update(dt)
	--turn CCW
	if love.keyboard.isDown("a") then
		spaceship.direction = spaceship.direction - spaceship.turnSpeed * dt
	end

	--turn CW
	if love.keyboard.isDown("d") then
		spaceship.direction = spaceship.direction + spaceship.turnSpeed * dt
	end

	--add force
	if love.keyboard.isDown("w") then
		spaceship.velX = spaceship.velX + math.sin(spaceship.direction) * spaceship.accel * dt
		spaceship.velY = spaceship.velY + math.cos(spaceship.direction) * -spaceship.accel * dt
	
	end

        --update the position
	spaceship.posX = spaceship.posX + spaceship.velX * dt
	spaceship.posY = spaceship.posY + spaceship.velY * dt
end

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
User avatar
TheMeq
Citizen
Posts: 56
Joined: Fri Sep 02, 2011 9:56 pm
Location: Nottingham, UK

Re: Realistic space flight

Post by TheMeq »

Thanks! But how do I backwards? I tried setting the pluses I minuses but it still goes forward on s press
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: Realistic space flight

Post by Beelz »

The only thing you do is invert the trig functions:

Code: Select all

if love.keyboard.isDown("s") then
		spaceship.velX = spaceship.velX + -math.sin(spaceship.direction) * spaceship.acceleration * dt
		spaceship.velY = spaceship.velY + -math.cos(spaceship.direction) * -spaceship.acceleration * dt
	end

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
User avatar
TheMeq
Citizen
Posts: 56
Joined: Fri Sep 02, 2011 9:56 pm
Location: Nottingham, UK

Re: Realistic space flight

Post by TheMeq »

Yus! Perfect!

Thank you so much!

One last thing, how can I make the ship start to slow down if it is not moving forward or backwards to a complete stop? I've tried changing the value of accelerate, but it still keeps moving. I think it needs a breaking force as well.
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: Realistic space flight

Post by bobbyjones »

You could use a dampening value that is always added to the velocity. After acceleration is stop the dampening value which should be negative will slow the ship down to 0 velocity
User avatar
Ovidios
Prole
Posts: 29
Joined: Thu Dec 04, 2014 12:00 pm
Location: Berlin, Germay

Re: Realistic space flight

Post by Ovidios »

bobbyjones wrote:You could use a dampening value that is always added to the velocity. After acceleration is stop the dampening value which should be negative will slow the ship down to 0 velocity
That wouldn't happen in space though... ^^
User avatar
zorg
Party member
Posts: 3450
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Realistic space flight

Post by zorg »

Ovidios wrote:
bobbyjones wrote:You could use a dampening value that is always added to the velocity. After acceleration is stop the dampening value which should be negative will slow the ship down to 0 velocity
That wouldn't happen in space though... ^^
On the other hand, spaceships having a "reorienting" system isn't that bad an idea :3
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: Realistic space flight

Post by Beelz »

zorg wrote:On the other hand, spaceships having a "reorienting" system isn't that bad an idea :3
This, great idea! I'll be adding that to my game later.
TheMeq wrote:One last thing, how can I make the ship start to slow down if it is not moving forward or backwards to a complete stop? I've tried changing the value of accelerate, but it still keeps moving. I think it needs a breaking force as well.
On this note, here is what I did for that... Its the same as the back thrust code except the force is toned down.

Code: Select all

spaceship.speed = (((spaceship.velX - spaceship.x)/2) + ((spaceship.velX - spaceship.x)/2))/2
if spaceship.speed > 0 then
      spaceship.velX = spaceship.velX - math.sin(spaceship.direction) * (spaceship.acceleration/5) * dt
      spaceship.velY = spaceship.velY - -math.cos(spaceship.direction) * (-spaceship.acceleration/5) * dt
end

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
User avatar
TheMeq
Citizen
Posts: 56
Joined: Fri Sep 02, 2011 9:56 pm
Location: Nottingham, UK

Re: Realistic space flight

Post by TheMeq »

I've noticed an issue with the velocity code, if the ship stays on course in a certain direction, the velocity values still increase until the ship is going at a 45 degree angle, have I done something else wrong?
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 3 guests