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.

Dark GDK / 2D Camera Question

Author
Message
Greg_C
14
Years of Service
User Offline
Joined: 22nd May 2010
Location:
Posted: 14th Jul 2010 23:15
So I have a working 2D camera and I also have the scaling of the camera working so I can zoom in and out and the position of my objects scale accordingly. So for the past couple of days I have tried to get two things to work for my camera.

First:
The first thing I am trying to do is create a boundary for my game. So for instance if my zoom value is 2. That means that I am scaling everything by 2 times its original size. So when I move my camera around I want the camera to stop moving if it hits the edge of my level. Now without scaling my camera wouldn't be able to move because my level is based off the original screen size which is 800x600.

So how would I be able to base my boundary off the scale amount?

Second:
I want the camera to move how it does in games like Mario where it doesn't move until Mario goes a certain distance forward and then it moves.

Not to sure how to do this has anyone tried this before? Or have a working example?
Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 16th Jul 2010 10:28 Edited at: 16th Jul 2010 10:32
You haven't really explained how your camera system works...

Basically, the idea of a "camera" is the idea of transforming world coordinates into screen coordinates. Thus, a check to keep a camera in boundaries should be simple, say "if(camera_x>20) camera x=20".

If you're doing something really odd for your camera transformations... which I'm guessing you are if you're having trouble with this... I'd suggest recoding everything. I have my favorite system all worked out; some code here: http://forum.thegamecreators.com/?m=forum_view&t=172082&b=6 (in DBPro but the idea should be fine).

There's crap for handling velocity and friction but that's not important, the important things are this:
base_pixels_per_unit=screen pixels per unit when zoomVal=0 (and therefore zoom=1)
zoom=exp(-zoomVal) <- this allows for nice, exponential zooming.
world units per screen width = base_pixels_per_unit*zoom
<camx, camy> = camera position x and y in world units
(<x,y> denotes a vector)

So now we have all the info we need to transform world coordinates to screen coordinates. The gist of that is this:
<x,y>=arbitrary point in world units
<x',y'>=point in screen space, from pixel <0,0> to <screen width,screen height>
<x',y'>=(<x,y>-<camx,camy>*base_pixels_per_unit*zoom+<screen width, screen height>/2.0

or as seen in my code:
ret.x=((x-cs_Pos.x)*(cs_PPU*cs_zoom))+0.5*screen width()
ret.y=-((y-cs_Pos.y)*(cs_PPU*cs_zoom))+0.5*screen height()



It may sound kinda weird, but thats because if you want to add in other transformations or other stuff, you should adhere to mathy standards and rules of dimensional analysis (IE that vector math above evaluates to <pixels,pixels> given <x,y> in <world units, world units>


So then, everything becomes simple!

[edit]
Also, you should have a function like "static const vec2 transformPointToScreen(vec2 world_coords);" so that - unless you're dealing with modifications to the camera system - you never have to muck about in screen space, you can always just say "make circle at <1,4>" and everything will be fine and dandy! That's the reason why it simplifies things once you're done.


Is't life, I ask, is't even prudence, to bore thyself and bore thy students?
Greg_C
14
Years of Service
User Offline
Joined: 22nd May 2010
Location:
Posted: 16th Jul 2010 19:53
Yeah thats a lot like how I have the 2D camera:



This is what I use to convert my screen coordinates to world coordinates. I was just wondering if there would be a way to create boundaries with out having to hard code it. Also I was wondering if it would be possible to make a bounding box based off the entire screen width and height and then check the position of that bounding box to see if it is hitting the new screen width and height based off the scale value.
HOW Dark GDK COR
13
Years of Service
User Offline
Joined: 18th Jul 2010
Location:
Posted: 18th Jul 2010 13:57
TEST, why cannot i post long text?

I've been in trouble with gdk, i dont know the GDK's Coordinate,It makes me very uncomfortable。i post my code and res, hope Master can hope me...
I am very sorry my English level, i wish u can know me

CODE:
#include "DarkGDK.h"

int Y_MAX = 0, X_MAX = 0;
float vx = 0, vy = 0, vz = 0;
int MapData[100][100];

bool Load_Map_data(void){
FILE *fp;
bool fok = false;

fp = fopen("map2.dat","r");
if (fp != NULL){
fscanf(fp,"%d",&Y_MAX);
fscanf(fp,"%d",&X_MAX);
int z;
for(z = Y_MAX-1; z > -1; z--) {
int x = 0;
//while(x++ < X_MAX){
for(x = 0; x < X_MAX; x++){
fscanf(fp,\"%d\",& MapData[z][x]);
}
}
fclose(fp);
}else{
return false;
}
//load res
dbLoadImage("res.bmp", 1);//block
dbColorBackdrop (dbRGB(0, 0, 0));
return true;
}
void DrawMap(){
dbMakeMatrix(1, 2*40, 2*40, X_MAX, Y_MAX);
dbPositionMatrix (1, 0, 0, 0);
dbPrepareMatrixTexture (1, 1, 2, 1);//
dbGhostMatrixOn (1);
for(int z = Y_MAX-1; z > -1; z--){
int x = 0;
//while(x++ < X_MAX){
for(x = 0; x < X_MAX; x++){
if(MapData[z][x] == 1){
dbSetMatrixTile(1, x, z, 1);//
}else if(MapData[z][x] == 0){
dbSetMatrixTile(1, x, z, 2);//
}
}
}
}

void UpdateMap(){
dbUpdateMatrix ( 1 );
}

void SetView(float x, float y, float z){
dbPositionCamera(x, y, z);
dbPointCamera (0, 0, 0);//??
}

void DarkGDK ( void ){
dbSyncOn ( );
dbSyncRate ( 60 );

if(!Load_Map_data())
return;
DrawMap();

vx = 0; vy = 0; vz = 0;
SetView(vx, vy, vz);

while ( LoopGDK ( ) ){
if ( dbEscapeKey ( ) )
return;

if(dbUpKey()){
vx = vx;
vy = 10;
vz += 10;
}
if(dbDownKey()){
vx = vx;
vy = 10;
vz -= 10;
}
if(dbLeftKey()){
vx -= 10;
vy = 10;
vz = vz;
}
if(dbRightKey()){
vx += 10;
vy = 10;
vz = vz;
}
SetView(vx, vy, vz);
UpdateMap();
dbSync ( );
}
return;
}

map data and bitmap res in Attachment

newer

Login to post a reply

Server time is: 2024-07-04 09:57:58
Your offset time is: 2024-07-04 09:57:58