Point in a direction and move in that direction

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
luaiscool
Prole
Posts: 34
Joined: Mon Apr 21, 2014 11:03 pm

Point in a direction and move in that direction

Post by luaiscool »

I've asked this question before but I said it wrong so I am re-posting it rewritten.

So,

I need some help with this, I want to point an image or in my case my ship(space not water) in a direction and move in that direction. The farthest I got was it to rotate and move on the Y and X axis. So the controls you be Up arrow goes forward, Down goes back, Right rotates clockwise and left rotates counter-clockwise. If you need to files to my "game" please ask and I will add that!

Thanks in advance!

Lua IS Cool!
http://xkcd.com/979/

Code: Select all

if signature = true then
    print(signaturetext)
else
    print("Error: Signature Not Found")
end
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: Point in a direction and move in that direction

Post by bobbyjones »

Do you need help with getting it to move in the direction it is facing?
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: Point in a direction and move in that direction

Post by bobbyjones »

I believe this can be done using slopes from algebra. For example to go to the top right corner you would do
Y = Y + (speed*slopeY*dt) slopeY would be 1
X = X + (speed*slopeX*dt) slopeX would be 1
To go horizontal to the right then slopeY will be 0 and slopeX will be 1. Now knowing this all you will need is a formula to calculate slope based off of the rotation. I don't know how rotation works in love. But if you I see your code I might be able to make a formula
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: Point in a direction and move in that direction

Post by bobbyjones »

When ever a the up key is pressed you increase both X and Y so with the last post put it both under love.keyboard.isdown ('up') I think that is right I have the auto complete so I don't be remembering the functions and things
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Point in a direction and move in that direction

Post by micha »

Here is some code, that does the job:

Code: Select all

function love.load()
  x,y = 100,100
  angle = 0
  image = love.graphics.newImage('image.png')
  speed = 50
  rotspeed = 1
end

function love.draw()
  love.graphics.draw(image,x,y,angle)
end

function love.update(dt)
  if love.keyboard.isDown('up') then
    x = x + math.cos(angle) * speed * dt
    y = y + math.sin(angle) * speed * dt
  end
  if love.keyboard.isDown('down') then
    x = x - math.cos(angle) * speed * dt
    y = y - math.sin(angle) * speed * dt
  end
  if love.keyboard.isDown('left') then
    angle = angle - rotspeed * dt
  end
  if love.keyboard.isDown('right') then
    angle = angle + rotspeed * dt
  end  
end
Note, that the image rotates around the upper left corner, unless you additionally supply the coordinates of the rotational center to the drawing command.
luaiscool
Prole
Posts: 34
Joined: Mon Apr 21, 2014 11:03 pm

Re: Point in a direction and move in that direction

Post by luaiscool »

HAD TEXT BUT I DELETED IT
Last edited by luaiscool on Wed Apr 30, 2014 11:54 pm, edited 1 time in total.
http://xkcd.com/979/

Code: Select all

if signature = true then
    print(signaturetext)
else
    print("Error: Signature Not Found")
end
luaiscool
Prole
Posts: 34
Joined: Mon Apr 21, 2014 11:03 pm

Re: Point in a direction and move in that direction

Post by luaiscool »

micha wrote:Here is some code, that does the job:
CODE
Note, that the image rotates around the upper left corner, unless you additionally supply the coordinates of the rotational center to the drawing command.
Can I ask for an explanation? I had to flip the image clockwise 90 degrees for the images to be moving correct but that's alright, So can you explain how to supply the coords of the rotational center?
http://xkcd.com/979/

Code: Select all

if signature = true then
    print(signaturetext)
else
    print("Error: Signature Not Found")
end
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Point in a direction and move in that direction

Post by davisdude »

Use the love.graphics.draw arguments. Note that love.graphics.draw is:

Code: Select all

love.graphics.draw( drawable, x, y, Rotation, ScaleX, ScaleY, OffsetX, OffsetY, ShearingX, ShearingY )
Make the offset 1/2 the width and height of the image.
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
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Point in a direction and move in that direction

Post by micha »

First of all, you had to rotate your image, because the way I implemented it, an angle of 0 means movement to the right. So the image should show the object pointing to the right. If you want it to point up, then you'd have to interchange the math.sin and the math.cos and put a minus before one of the two (ad hoc, I don't know which one, just try, if you are interested).

Now for the center of the origin: love.draw accepts very many input parameters:

Code: Select all

love.graphics.draw( drawable, x, y, r, sx, sy, ox, oy, kx, ky )
The coordinates of the origin are ox and oy. We cannot supply these, without also supplying sx and sy, so the new drawing call you need is:

Code: Select all

love.graphics.draw(image,x,y,angle,1,1,ox,oy)
(kx and ky are set to a useful default value). We set sx and sy to 1, so that the image is not stretched/scaled.

Now for the values of ox and oy you insert the coordinates inside the image, where the center is. For example, if you have an image of size 32x32 pixels, then you would set ox=oy=16 to get the center of the image. But if you for example have a very asymetric object, the center of rotation may be any point in (or outside) the picture.

Edit: davisdude was faster!
Post Reply

Who is online

Users browsing this forum: No registered users and 75 guests