move an object forward at its current angle ?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
PGUp
Party member
Posts: 105
Joined: Fri Apr 21, 2017 9:17 am

move an object forward at its current angle ?

Post by PGUp »

so, to make it simple, let say that every 1 second a rectangle will spawn at a random position, and at random angle too...
i already make some codes to spawn it, but i have no idea how to make the rectangle move forward at its current angle, help ?
-
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: move an object forward at its current angle ?

Post by raidho36 »

You can use trigonometic functions, sine and cosine, to calculate X and Y component of a unit vector pointing in that direction. Then you can multiply it by speed and it'll move X fraction of the speed on X axis and Y fraction of the speed on Y axis.
PGUp
Party member
Posts: 105
Joined: Fri Apr 21, 2017 9:17 am

Re: move an object forward at its current angle ?

Post by PGUp »

raidho36 wrote: Sat Apr 29, 2017 2:00 pm You can use trigonometic functions, sine and cosine, to calculate X and Y component of a unit vector pointing in that direction. Then you can multiply it by speed and it'll move X fraction of the speed on X axis and Y fraction of the speed on Y axis.
umm.. can you give me the code ?
-
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: move an object forward at its current angle ?

Post by raidho36 »

Code: Select all

rect.x = rect.x + math.cos ( rect.angle ) * rect.speed * dt
rect.y = rect.y - math.sin ( rect.angle ) * rect.speed * dt
Sine sign is opposite because 2d computer graphics convention has Y axis inverted.
MasterLee
Party member
Posts: 141
Joined: Tue Mar 07, 2017 4:03 pm
Contact:

Re: move an object forward at its current angle ?

Post by MasterLee »

raidho36 wrote: Sun Apr 30, 2017 4:13 am Sine sign is opposite because 2d computer graphics convention has Y axis inverted.
Actually it is that way because English is written left to right and top to down. Also TV monitor refresh is left to right and top to down. So this was an pretty obvious choice. But other system not tied to TV monitors for example Vectrex have the y coordinate the other way round. Maybe that was because it was based on devices that draw graphs.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: move an object forward at its current angle ?

Post by raidho36 »

Mathematical convention is Y pointing upwards, and all math functions work that way. Note that this also applies to 3d graphics, where Y is also upwards. Only 2d graphics have Y axis inverted by convention.
MasterLee
Party member
Posts: 141
Joined: Tue Mar 07, 2017 4:03 pm
Contact:

Re: move an object forward at its current angle ?

Post by MasterLee »

Also monitors are basically an dot matrix (with stereo sound). And matrices are indexed from top to down and left to right.
Also computers are used for math only by few people. IBM PC compatibles main usage is:
Image
On the other hand no one keeps you from inverting the y-Axis. (Want your y-Axis inverted of Operation system level basis use OS/2 or MacOS and not Windows or Linux.)
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: move an object forward at its current angle ?

Post by raidho36 »

Which is a shame really. Computers are meant to do calculation-heavy tasks, not help file your paperwork - which only spawned more paperwork ultimately.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Vixii and 86 guests