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!
Point in a direction and move in that direction
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Point in a direction and move in that direction
http://xkcd.com/979/
Code: Select all
if signature = true then
print(signaturetext)
else
print("Error: Signature Not Found")
end
-
- Party member
- Posts: 730
- Joined: Sat Apr 26, 2014 7:46 pm
Re: Point in a direction and move in that direction
Do you need help with getting it to move in the direction it is facing?
-
- Party member
- Posts: 730
- Joined: Sat Apr 26, 2014 7:46 pm
Re: Point in a direction and move in that direction
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
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
-
- Party member
- Posts: 730
- Joined: Sat Apr 26, 2014 7:46 pm
Re: Point in a direction and move in that direction
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
Re: Point in a direction and move in that direction
Here is some code, that does the job:
Note, that the image rotates around the upper left corner, unless you additionally supply the coordinates of the rotational center to the drawing command.
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
Check out my blog on gamedev
Re: Point in a direction and move in that direction
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
Re: Point in a direction and move in that direction
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?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.
http://xkcd.com/979/
Code: Select all
if signature = true then
print(signaturetext)
else
print("Error: Signature Not Found")
end
Re: Point in a direction and move in that direction
Use the love.graphics.draw arguments. Note that love.graphics.draw is:
Make the offset 1/2 the width and height of the image.
Code: Select all
love.graphics.draw( drawable, x, y, Rotation, ScaleX, ScaleY, OffsetX, OffsetY, ShearingX, ShearingY )
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
Re: Point in a direction and move in that direction
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:
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:
(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!
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 )
Code: Select all
love.graphics.draw(image,x,y,angle,1,1,ox,oy)
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!
Check out my blog on gamedev
Who is online
Users browsing this forum: No registered users and 75 guests