Page 1 of 1
Degrees with SDL coordinate system
Posted: Thu Mar 25, 2010 2:04 pm
by Sanshin77
Hey, we started trigonometry in math this year. I sat down a few days ago and created a function calculating the angle, in degrees(0-360), from one point to another. I have tested my function thoroughly(in all "4 main directions"), and it seems to work. I started thinking about using this for direction of objects in games and I wondered what is the best way to go about doing this with SDL's coordinate system.
I made a simple drawing to kinda illustrate my question:
It's natural for me to think that 90 degrees is straight up, 0 is straight to the right, 135 is diagonally up to the left, 270 is straight down and so on. Should I translate the angle(in degrees) in my function so that 90 is in fact straight up( on-screen ) or should I keep it as it is, 90 = straight down, higher y value?
Re: Degrees with SDL coordinate system
Posted: Thu Mar 25, 2010 3:31 pm
by xiphirx
I would suggest that you make it so it seems natural to you...
So, going up would mean an angle of 90.
Re: Degrees with SDL coordinate system
Posted: Thu Mar 25, 2010 7:10 pm
by Falco Girgis
I, personally, would be consistent with the unit circle.
Re: Degrees with SDL coordinate system
Posted: Fri Mar 26, 2010 7:12 am
by Arce
GyroVorbis wrote:I, personally, would be consistent with the unit circle.
Agree.
So, just treat it the same as a Cartesian coordinate system, with the exception of an inverted y-axis.
So, if you want a projectile to fly NorthEast in SDL, I'd make my calls something like this:
Code: Select all
bullet.y+= sin( (-1)*(45) ) * bull.speed;
bullet.x+= cos( 0 ) * bullet.speed;
or for a generic direction something like
Code: Select all
bullet.y+= sin( (-1)*(bullet.direction) ) * bull.speed;
bullet.x+= cos( bullet.direction ) * bullet.speed;
Note how I just multiplied bullet.direction in the call to sin by negative one to account for the inverted axis.
Re: Degrees with SDL coordinate system
Posted: Sat Mar 27, 2010 6:24 pm
by short
when doing this just remember how computationally expensive sin and cos really are..
Re: Degrees with SDL coordinate system
Posted: Sat Mar 27, 2010 7:49 pm
by Ginto8
Why use angles when you can use vectors? Yes, it could be an option to generate a vector at a certain angle, but other than that, there is little to no advantage (and plenty of disadvantages - mainly that trig is so expensive to calculate) that comes from using angles over vectors. I mean, it's cool and all, but it's not really a good idea in the long run.
Re: Degrees with SDL coordinate system
Posted: Wed May 19, 2010 10:54 am
by Sanshin77
Sorry for not replying, my comp. was at repair for like a month (stupid ash-cloud preventing part transport) and now I had exams, so I haven't been around or devved a lot.
Ginto8 wrote:Why use angles when you can use vectors? Yes, it could be an option to generate a vector at a certain angle, but other than that, there is little to no advantage (and plenty of disadvantages - mainly that trig is so expensive to calculate) that comes from using angles over vectors. I mean, it's cool and all, but it's not really a good idea in the long run.
Yeah, I actually have a good system working with vectors now, I saw your reply and changed my mind. This will be featured in my next youtube video, the 4th episode of my Amateur Game Dev series, along with some simple collision/physics... :D