am I doing this right?

BlitzMax Forums/BlitzMax Beginners Area/am I doing this right?

Cruis.In(Posted 2006) [#1]
Ok I am making a 2d top down space game, where you ship is fixed at the center of the screen, and you fly around the game world.

presently, I have used
setorigin (graphicswidth()/2 - x , graphicsheight()/2 - y )


where x and y are your ship's x and y. I have done this to keep your ship drawn in the center of the screen so it can move about the game world.

otherwise without that, the ship flies off the edge of the screen.

so what I want to know is, is this the correct way to have enabled flying / being in the gameworld? or will this cause me problems later on and do I need to make some sort of scrolling background engine thing.

i experimented originally with creating a viewport, and as the ship moved, moving the viewport whilst keeping the ship at the centre, so the viewport would basically be = to the current width.height of the screen and there fore your ship would never get clipped for flying off the side, because the viewport like a camera moves over the ship to keep it in view, or to keep drawing the area of the gameworld you are in.


also if setorigin is not supposed to be used in this way, what was it meant for?

was I clear in my explanation or do I need to try again


ImaginaryHuman(Posted 2006) [#2]
You're confused about what these commands are for.

SetOrigin changes where the X=0,Y=0 coordinate is. Usually it's the top left corner of the screen. What you COULD do is say, if the player wants to fly to the right, then you start subtracting values from the X coordinate of the origin, while at the same time adding values to the X coordinate of the player ship - but not to the enemy ships. Your enemy ships will start to scroll to the left and your player ship will stay where it is - so long as you move it to the right the same amount as you're moving the origin to the left. Same for vertical.

That IS one way of doing it. Presumably you are only drawing enemies that are within the visible part of the game world, though?

Another way is to leave the origin at 0,0 and have your own GameWorldOriginX,GameWorldOriginY coordinates that you use to move things around. You then can always draw the player in the middle of the screen and just rotate it, when the player flies to the right you still add to its X coordinate, but then you move your `game world window` to the right, so that you only see the visible portion centered on the ship.

You should not need to touch SetViewport and definitely should have nothing to do with your scrolling.

SetViewport is misleading, anyway. In OpenGL for example, the viewport is the `output window` for the entire defined `view` of the game world. The size of the view of the game world is set up separately as a projection, either orthographic or perspective. The coordinates from the game world are scaled to fit within the viewport, regardless of the viewport size, so the viewport is really just the `output size` of the game world view, and the view on the world is something entirely separate that Max2D doesn't give you access to. In Max the viewport is thought of more as a way to stencil certain areas of the screen without changing the size of the view of the game world, or the size of the overall game screen. If you look at the code it actually sets up a scissor stencil for the rectangle you specify, so you only see drawing to that area, it actually is more like the old Amiga version of viewports. Internally it's doing something quite different from what a viewport really is in OpenGL terms.

I think you'll find that most people do not use SetOrigin to scroll their game world, they more likely have their own origin and game world window offset based on the player coords, just like drawing a portion of a tilemap.


Cruis.In(Posted 2006) [#3]
hi angel daniel thanks for your response. you said

What you COULD do is say, if the player wants to fly to the right, then you start subtracting values from the X coordinate of the origin


I originally posed this code
setorigin (graphicswidth()/2 - x , graphicsheight()/2 - y )


am I right in saying that this code is doing what you said? as its doing a subtracting calculation with x changing as you move the ship.


Presumably you are only drawing enemies that are within the visible part of the game world, though?/quote]

Actually I have drawn some enemies like x= 2000, y = 2000 and fly to meet them.

I have my ship,x displayed and my ship,y so i can see when I am nearing the 2000,2000 and also to help navigate :)

and when I get there the enemy ship is there waiting. Did you expect it wouldn't work? As in you didn't believe what I was doing could work? Or were you just asking.

Isnt the part you suggested with the gameworldOriginX and Y the same as with your first suggestion;
subtracting a value from the x coordinate of the origin and adding to the x coord of the playership?


[quote]if the player wants to fly to the right, then you start subtracting values from the X coordinate of the origin, while at the same time adding values to the X coordinate of the player ship - but not to the enemy ships


Also correct me if I am wrong now but isn't the above paragraph quote actually what I am doing? because-

I am subtracting a value from the x coordinate and y coordinate of setorigin with the code
setorigin (graphicswidth()/2 -x )
same for the Y coord of the setorigin.

AND i didnt mention before, of course I am adding to the x and y value of the ship based on how the player moves. And the x and y in that above code is the ship.x and ship.y, and this also means that the value being taken away from the x and y origin is the same as that being added to the ship. so the ship remains in the middle. Just like you explained

so the origin only moves when you move as in input(up,down,left right keys), which is how it should be right?

what I have done to limit the game world or create the box that the game world now is, is multiply world_width = screen_width * 2000.
which is fairly big.


let me know if I am right, in that what you wrote to do , is what I am actually doing, just didnt add in the part about the ship.

also then if you want to set something back to normal like for drawing interface graphics, you would have to put a setorigin 0,0 command back in the loop in that area before drawing them otherwise the origin would be being calculated with the other setorigin call, and this would be un wanted.


ImaginaryHuman(Posted 2006) [#4]
Sounds right


ImaginaryHuman(Posted 2006) [#5]
Sounds right