[Solved] Spaceship movement (rotational)

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.
Post Reply
Kripis
Prole
Posts: 5
Joined: Sun Jul 07, 2013 10:08 pm

[Solved] Spaceship movement (rotational)

Post by Kripis »

I do not understand what is wrong with this picture I mean please help me!

http://pastebin.com/QDVf9xHz :|
Last edited by Kripis on Mon Jul 08, 2013 11:00 pm, edited 2 times in total.
User avatar
chezrom
Citizen
Posts: 59
Joined: Tue May 28, 2013 11:03 pm
Location: France

Re: Spaceship movement (rotational)

Post by chezrom »

Please be more precise with your question.

I saw some mistakes in player.lua :
  • In which unit are your angles ? love.graphics.draw waits an angle in radian, so if you want your angle in degree you must do also a conversion here.
    Personnaly I use all angle in radians, to limit conversion.
  • I think the coordinate of the player muste be player.x & player.y in love.graphics
  • For information, an angle of zero is horizontal (pointing to the right), when angle increase, it turns clockwise (y go downward)
  • When you use a angle with love.graphics.draw you must be aware than by default it rotates the image around the left upper corner.
    You must precise an origin, by example :

    Code: Select all

    love.graphics.draw(
      player.sprite, 
      player.x, player.y, 
      player.angle+math.pi/2,
      1,1,
      player.sprite:getWidth()/2,player.sprite:getHeight()/2)
    
    (In this code I user angle in radian, and Add pi/2 (=90°) because by default your image is vertical)
  • Don't add dt to your angle,because in degree you turn 1 degree per second !!! It's very slow (In Naev/Epiar, the start ship turn 130°/sec)
    You can define player.anglespeed to be the angular speed (for example 180°/seconde or pi radians/second) and add/substract to your angle dt*player.anglespeed
  • Your physical model behave strangely and the fact that you change the velocity directly look very strange. To be more natural, use a model with acceleration.
Here is a listing of player.lua that I modified, with the original physic model :

Code: Select all

player = {}
player.sprite = love.graphics.newImage("graphics/player.png")
player.speed = 225
player.friction = 0.2
player.x = 100
player.y = 100
player.xvel = 0
player.yvel = 0
player.angle = 0
player.anglespeed=math.pi

function player.load()
	width = love.graphics.getWidth()
	height = love.graphics.getHeight()
end


function player.move(dt)
	if love.keyboard.isDown("a") then
		player.angle = player.angle - player.anglespeed*dt
	end
	if love.keyboard.isDown("d") then
		player.angle = player.angle + player.anglespeed*dt
	end
	if love.keyboard.isDown("w") then
		player.xvel = math.cos(player.angle)*player.speed
		player.yvel = math.sin(player.angle)*player.speed
	end
	if love.keyboard.isDown("s") then
		player.xvel = -math.cos(player.angle)*player.speed
		player.yvel = -math.sin(player.angle)*player.speed
	end
	
	player.x = player.x + player.xvel*dt
	player.y = player.y + player.yvel*dt
	player.xvel = player.xvel * (1 - math.min(dt * player.friction, 1))
	player.yvel = player.yvel * (1 - math.min(dt * player.friction, 1))
end


function player.update(dt)
	player.move(dt)
end

function player.draw()
	love.graphics.draw(player.sprite, player.x, player.y, player.angle+math.pi/2,1,1,player.sprite:getWidth()/2,player.sprite:getHeight()/2)
end
For the physic model, I'll write a post tomorrow, if possible.
Kripis
Prole
Posts: 5
Joined: Sun Jul 07, 2013 10:08 pm

Re: Spaceship movement (rotational)

Post by Kripis »

chezrom wrote:snip
Wow thanks alot I learned quite a bit from your post, but I do have a question how can i make it be vertical by default, so 90° ?
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Spaceship movement (rotational)

Post by Plu »

You can't change the default orientation, you'll just have to set the initial value of your rotation variable to 90 degrees.
User avatar
chezrom
Citizen
Posts: 59
Joined: Tue May 28, 2013 11:03 pm
Location: France

Re: Spaceship movement (rotational)

Post by chezrom »

Kripis wrote:Wow thanks alot I learned quite a bit from your post, but I do have a question how can i make it be vertical by default, so 90° ?
You can set player.angle to -90° as default value (or -pi/2 in radians), to have initialy your ship pointing upward.
I assume that player.angle is the angle between the horizontal and the direction of the ship.

But you can also define player.angle to be the angle between the upward vertical and the direction of the ship.
(the angle always increase clockwise, angle=+90° is horizontal pointing to the right)
You must change your formulas (welcome trigonometry ! It's uneasy without a picture) :

Code: Select all

  vx = sin(angle) * speed
  vy = -cos(angle) * speed
In this case, you can pass directly this angle to the rotation for love.graphics.draw.
(modified player.lua : http://pastebin.com/C6gVYcAz)
I hope I don't confuse you.

NOTE : for the origin point in love.graphics.draw, the center of image in an example : you must give here values that allow rotation to look natural (for your ship, the 'natural' center of rotation seems to be lower than the center of image).
In addition this is this point that is at (x,y) on the screen (can be usefull for collision detection).
Plu wrote:You can't change the default orientation, you'll just have to set the initial value of your rotation variable to 90 degrees.
In this case, the only thing that you can't change is how love.graphics.draw understands its arguments. For this function the rotation is in radian, in clockwise orientation. You can define your angle (direction) as you want, but you must be coherent in your computations.
User avatar
chezrom
Citizen
Posts: 59
Joined: Tue May 28, 2013 11:03 pm
Location: France

Re: Spaceship movement (rotational)

Post by chezrom »

Sorry for the double post, but I want to speak about a simple physic model with acceleration and friction.

Without Friction
Acceleration is to speed what speed is to position, a modification rate by time unit.
In a simple physic model, force cause acceleration.

For the ship, when you turn on your reactor, a force is exerced on the ship, the thrust. This thrust causes an acceleration in the current direction of the ship, and in our purpose we can say it's constant, for example 100. An acceleration of 100 means if exerced during 1 s the speed go from 0 to 100 pixel/s.

If you don't press the touch 'W' the acceleration will be zero.

The code for this :

Code: Select all

function player.move(dt)
   local ax,ay=0,0
   if love.keyboard.isDown("a") then
      player.angle = player.angle - player.anglespeed*dt
   end
   if love.keyboard.isDown("d") then
      player.angle = player.angle + player.anglespeed*dt
   end
   if love.keyboard.isDown("w") then
      ax = math.sin(player.angle)*player.thrust
      ay = -math.cos(player.angle)*player.thrust
   end
   player.xvel = player.xvel + ax*dt
   player.yvel = player.yvel + ay*dt
   player.x = player.x + player.xvel*dt
   player.y = player.y + player.yvel*dt
end
(I use the convention player.angle = 0 if the ship point upward).
player.thrust is the amount of acceleation when reactor is on, for example I use 100.
To resume :
  • We compute acceleration in the direction of the ship if we turn reactor on
  • With this acceleration we modify the velocity of the ship.
  • With the new speed, we modify the position of the ship
  • We use dt when modifying speed & position because we compute the move on dt seconds
Now the ship has a movement as in the game asteroid. It keep its speed, and you must do some operations to stop the ship.

With Friction

OK in space, you have no friction, but we are not in reality but in game, so if it helps the gameplay we can had friction.
Friction is a force, related to the speed of theship, that causes a new acceleration.
So the total acceleration of the ship is the vector total of the thrust (if any) and of the friction.

To keep thing simple, the more easier friction function to use is a proportion : vector(friction act) = - coeff * vector(ship speed)
"coeff" is positive, the friction acceleration vector is opposed to the ship speed vector, because it decreases speed.

In code you have after the key check, player.friction is the friction coeff :

Code: Select all

   ax = ax - player.xvel * player.friction
   ay = ay - player.yvel * player.friction
Which must be value of player.friction ?
You can test values.
Another way is to set a maximum speed for your ship. At this speed, the thrust only maintain the speed of the ship, the sum of the thrust vector and the friction acceleration vection is zero. So you have the relation : maxSpeed * coeff = thrust, so :

Code: Select all

  player.friction = player.thrust/player.maxspeed
Here's player.lua with the new physic model and friction : http://pastebin.com/Mpz6jzkJ
Kripis
Prole
Posts: 5
Joined: Sun Jul 07, 2013 10:08 pm

Re: Spaceship movement (rotational)

Post by Kripis »

chezrom wrote:snip
I cannot express how thankful I'am, your name will be in the credits of my game, Cheers!

EDIT 1: I do have 1 more question, how can I apply friction to the rotation?
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: [Solved] Spaceship movement (rotational)

Post by davisdude »

You may want to change the title away from solved, since you still have a question...
I do have 1 more question, how can I apply friction to the rotation?
Math.

First, there are two types of friction: Static friction (exerted on the object when it's at rest) and kinetic friction (friction on an object in motion).
I assume you don't care about the first, since you are moving the object.

The formula for Kinetic Friction is:
Kinetic Force = coefficient of Friction * Force
Force = Mass * acceleration

Since it's on a flat plane (I'm assuming) the coefficient of friction is whatever materials you want to do research for... ice on steel, rubber on steel, etx.

kF = Kinetic Force
cF = coefficient of Friciton
From that you can determine:
kF = cF * ( Mass * Acceleration )

Mass would be the mass used to apply friction, Acceleration would be the speed it's rotating at. It's up to you to research the weights and things (or just make things up until you think it looks good :) )
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Post Reply

Who is online

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