Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] 2d world coordinates to camera coordinates!

Author
Message
Neuro Fuzzy
16
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 6th Sep 2009 02:54 Edited at: 8th Sep 2009 00:52
What to i mean by "2d world coordinates to camera coordinates"? Well... Yknow how in 3d DBPro apps, you position your objects in the world, and then you move your camera around? Why not place your 2d objects in the world, and then move your camera around?

Here's some code that does that. Basically, here's what you do:

1. Take all the units with their world coordinates
2. Translate all those points so that the camera is at 0,0
3. Rotate all those points so that the camera has 0 rotation
4. Divide those units by the scale factor (units per screen width) to get Screen widths, and add half a screen width to that.
5. Multiply that by pixels per screen width, and you have coordinates of any object in world coordinates, on the screen!
Rotation and translation:
x'=cos(-camtheta)*(x-camx)-sin(-camtheta)*(y-camy)
y'=sin(-camtheta)*(x-camx)+cos(-camtheta)*(y-camy)
Throw in unit conversion...
x'=width*((cos(-camtheta)*(x-camx)-sin(-camtheta)*(y-camy))/scale)+0.5*width
y'=width*((sin(-camtheta)*(x-camx)+cos(-camtheta)*(y-camy))/scale)+0.5*height

where (camx,camy) is the camera's position, camtheta is the camera's rotation, (x,y) is the object's position, and width and height are the width and height of the screen in pixels.

here's some code implementing that:



This is meh first working app using matrices, woopoo!
AndrewT
17
Years of Service
User Offline
Joined: 11th Feb 2007
Location: MI, USA
Posted: 8th Sep 2009 00:52 Edited at: 8th Sep 2009 00:53
Very cool! I've been wanting to implement something like this for awhile--it seems like it'd be very useful for top-down RTSes and the like, where zooming in and rotating the view is necessary.

On a side note, would you mind if I converted this to BlitzMax code and posted it in their code archives? Credit would of course be given. Thanks.

i like orange
Neuro Fuzzy
16
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 8th Sep 2009 08:21
Quote: "On a side note, would you mind if I converted this to BlitzMax code and posted it in their code archives? Credit would of course be given."


that wouldn't be a problem

The only thing that concerns me is that it's slow. I would imagine if it was in c++ or if the sin and cos commands were replaced with a lookup to an array, then it would be sped up alot.
Neuro Fuzzy
16
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 9th Sep 2009 06:41
nope, i tried replacing the trig commands with an array, there was barely any increase in speed.
Neuro Fuzzy
16
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 27th Sep 2009 06:06


I made a few optimizations to the code. Some things were being calculated more than they needed to be.
jackCurtis
14
Years of Service
User Offline
Joined: 3rd Nov 2009
Location:
Posted: 4th Nov 2009 00:16
I'm still not sure what you mean by "2d world coordinates to camera coordinates". Are you saying we'd place a sprite or something at the usual X,Y window coordinates, & your code will enable it to stick to a particular point in a movable terrain?

Unbeliever in the UnHolyTrinity of: Usury, Fake Democracy, & the Beast of Armageddon
jackCurtis
14
Years of Service
User Offline
Joined: 3rd Nov 2009
Location:
Posted: 4th Nov 2009 01:34
What kind of code is this? (doesn't look like Fortran

Unbeliever in the UnHolyTrinity of: Usury, Fake Democracy, & the Beast of Armageddon
jackCurtis
14
Years of Service
User Offline
Joined: 3rd Nov 2009
Location:
Posted: 5th Nov 2009 03:14
If that's what it is (sort if a reversible Mercator projection thing) I'd like to try to translate into C++, too. ( is it C#? I see a lot of #s )

Unbeliever in the UnHolyTrinity of: Usury, Fake Democracy, & the Beast of Armageddon

Login to post a reply

Server time is: 2024-05-03 00:57:51
Your offset time is: 2024-05-03 00:57:51