Degrees with SDL coordinate system

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Sanshin77
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Tue Mar 10, 2009 9:36 am
Current Project: C++/SDL engine, zaActionWizardMagic game
Favorite Gaming Platforms: Xbox 360, Playstation 2, Nintendo DS, mac and PC
Programming Language of Choice: C++

Degrees with SDL coordinate system

Post 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:
Image

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?
Check out videos of my C++ games as well as my "Amateur Game Dev" series over at
My YouTube Channel: http://www.youtube.com/user/Zanchill
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: Degrees with SDL coordinate system

Post by xiphirx »

I would suggest that you make it so it seems natural to you...

So, going up would mean an angle of 90.
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Degrees with SDL coordinate system

Post by Falco Girgis »

I, personally, would be consistent with the unit circle.
User avatar
Arce
Jealous Self-Righteous Prick
Jealous Self-Righteous Prick
Posts: 2153
Joined: Mon Jul 10, 2006 9:29 pm

Re: Degrees with SDL coordinate system

Post 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.
<qpHalcy0n> decided to paint the office, now i'm high and my hands hurt
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: Degrees with SDL coordinate system

Post by short »

when doing this just remember how computationally expensive sin and cos really are..
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Degrees with SDL coordinate system

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
Sanshin77
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Tue Mar 10, 2009 9:36 am
Current Project: C++/SDL engine, zaActionWizardMagic game
Favorite Gaming Platforms: Xbox 360, Playstation 2, Nintendo DS, mac and PC
Programming Language of Choice: C++

Re: Degrees with SDL coordinate system

Post 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
Check out videos of my C++ games as well as my "Amateur Game Dev" series over at
My YouTube Channel: http://www.youtube.com/user/Zanchill
Post Reply