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?
Realistic space flight
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Realistic space flight
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:
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
Re: Realistic space flight
Thanks! But how do I backwards? I tried setting the pluses I minuses but it still goes forward on s press
Re: Realistic space flight
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
Re: Realistic space flight
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.
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.
-
- Party member
- Posts: 730
- Joined: Sat Apr 26, 2014 7:46 pm
Re: Realistic space flight
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
Re: Realistic space flight
That wouldn't happen in space though...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
![Glee ^^](./images/smilies/ms-glee.png)
- zorg
- Party member
- Posts: 3450
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Realistic space flight
On the other hand, spaceships having a "reorienting" system isn't that bad an ideaOvidios wrote:That wouldn't happen in space though...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
![Awww :3](./images/smilies/ms-three.png)
Me and my stuff
True 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.
![Awww :3](./images/smilies/ms-three.png)
Re: Realistic space flight
This, great idea! I'll be adding that to my game later.zorg wrote:On the other hand, spaceships having a "reorienting" system isn't that bad an idea
On this note, here is what I did for that... Its the same as the back thrust code except the force is toned down.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.
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
Re: Realistic space flight
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?
Who is online
Users browsing this forum: Bing [Bot] and 3 guests