Players start position

BlitzMax Forums/BlitzMax Beginners Area/Players start position

BlitzMan(Posted 2016) [#1]
Hi guys
I have created a 2d map editor with a play function so i can play the game in the editor.
The tiles are 32x32 in size.If the player start position is 132x768 how on earth do i find his start position.He always starts at 0,0.Any help will be lovely.

tnx

p.s I have a tile that places the player start.


Hotshot2005(Posted 2016) [#2]
if you want player start 132 and 768 then you type

Player_X=132
Player_Y=768

Drawimage The_Bugger,Player_X,Player_Y

IF You using Functions then dont forget used Global for Player_X and Player_Y

Hope that help!


BlitzMan(Posted 2016) [#3]
Started this in the wrong forum SORRY.can you move it to BlitzMax Programming please.


Brucey(Posted 2016) [#4]
So you want his start position as the tile coordinates?

SuperStrict

Framework brl.standardio


Local x:Int = 132
Local y:Int = 768

Local tx:Int, ty:Int

ScreenToTile(x, y, tx, ty)

Print x + ", " + y + " -> " + tx + ", " + ty


Rem
bbdoc: Converts screen coordinates to tile coordinates.
End Rem
Function ScreenToTile(x:Int, y:Int, tx:Int Var, ty:Int Var, offX:Int = 0, offY:Int = 0)
	tx = x / 32 + offX
	ty = y / 32 + offY
End Function



BlitzMan(Posted 2016) [#5]
Forgot to mention i have eight way scrolling in this.Does it matter.

You say screen to til there is no size


Brucey(Posted 2016) [#6]
Hmm. Maybe I don't understand what you are referring to when you say:
If the player start position is 132x768 how on earth do i find his start position

because it doesn't really make much sense :-)


Dabhand(Posted 2016) [#7]
I usually use bitshifting for tilemaps, so if I wanted the player to start in a specific cell of the tilemap, say, at map[10,3], I would use:-

PlayerX = 10 shl 5
PlayerY = 3 shl 5

Likewise, you can use 'shr' to convert actual coordinates back to tilemap friendly numbers like so:-

tile = map[playerX shr 5, playerY shr 5]

Obviously add/subtract any offset accordingly.

This is what I used for my monkey croco remake:-

http://www.monkey-x.com/Apps/app.php?id=51

The bonus is since your playing with bits it becomes an instant optimisation... well, it used to be... Back in the day! :D

Dabz


BlitzMan(Posted 2016) [#8]
Sorry dont think i explained clealy.
The map tiles are 32x32 pixels in size.The map size is 132 map tiles wide.
and 768 map tiles depth.
I am getting confused myself here just typing it.Ha

thanks for any help guys.


BlitzMan(Posted 2016) [#9]
.


Bobysait(Posted 2016) [#10]

If the player start position is 132x768 how on earth do i find his start position.He always starts at 0,0.Any help will be lovely.



well ... "on earth", you have to convert to polar coordinates using Greenwich as a 0 position for longitude and equator for lattitude :)

Without joking, you're really confusing.
your Map is defined in Tile unit. If I understand : 132,768 (in Tile coords System)
The position of your player is in ... pixel ? Tile unit ? something else ?
And you want it in Pixel ? Tile Unit ?

BTW : if whatever you set the player start at 0,0, then you should try to follow your code from the moment you set the position to the moment it is drawn on screen. You may find something that force the position to be 0,0 whatever you set.
If you're using a framework, maybe it use a function/method to set the player position.


Else : according to general map coordinates, conversion from Tile to World (in pixel) would be :


' relative position to Tile (in pixel)
Function convertPixelCoordToTilePixel(pixel_coord:Int, TileSize:Int, MapOffsetInPixel:Int)
	Return pixel_coord - ( Floor ( Float(pixel_coord-MapOffsetInPixel)/TileSize ) * TileSize )
End Function

' global tile position (in Tile unit)
Function convertPixelToTileUnit:Int(pixel_coord:Int, TileSize:Int, MapOffsetInPixel:Int)
	Return Floor(Float(pixel_coord - MapOffsetInPixel)/TileSize)
End Function

' Tile Unit position to Pixel position
Function convertTileToPixel:Int(TilePos:Int, TileSize:Int, MapOffsetInPixel:Int)
	Return TilePos * TileSize + MapOffsetInPixel
End Function


"convertPixelCoordToTilePixel" transforms a global (world space) pixel coordinate to its Tile relative coordinate (ie : the number pixel from the edges of the Tile the player is on)

"convertPixelToTileUnit" transforms your pixel coordinate (world space) to tile unit (ie : the number of tile to reach the position)

"convertTileToPixel" transforms the coordinates the a Tile (in Tile Unit) to world space coordinates (in pixel)

So, if you want the player to start on the tile 4,7 (for example), you'll have to do this (with TILE_SIZE = 32 pixel and offset for the map=0)
Player_X = convertTileToPixel(4, TILE_SIZE, 0)
Player_Y = convertTileToPixel(8, TILE_SIZE, 0)

if you want the player to start in the middle of the title
Player_X = convertTileToPixel(4, TILE_SIZE, 0) + TILE_SIZE/2
Player_Y = convertTileToPixel(8, TILE_SIZE, 0) + TILE_SIZE/2

if you want to get the Tile Position of your player while its position is expressed in pixel (let's say, x=842, y=4213) :
Player_TileX = convertPixelToTileUnit(842, TILE_SIZE, 0)
Player_TileY = convertPixelToTileUnit(4213, TILE_SIZE, 0)


All is a matter of what unit you want to transform to what unit, and which coord system you use.


ps : if you're using a 2D framework, you may tell us the name, it could help.


Derron(Posted 2016) [#11]
Just a short interruption:

Floor ( Float(pixel_coord-MapOffsetInPixel)/TileSize )


isnt "int/int" already floor-truncated?
So if you do not "ceil()", you could just skip the whole "floor(float(int)/int)" and just return "int/int".

Also I am not sure if "Float(x)" is a wrapper to "x.ToFloat()" (aka: which one calls less things (there are others checking the resulting ASM-code...) :-).


Nonetheless the general post is a good one, above's question just came into my mind when scanning the code.


bye
Ron


Bobysait(Posted 2016) [#12]
You're right, just checked in Blitzmax : int/int is already floored.

It's just that I had never looked the result with blitzmax :)
In doubt, I prefered force it to be a floor. I only know some compilers that truncate before or after 0.5 (0.5 included or not)

Whatever I'm not really sure the positioning of a single player could affect anything of the framerate, you're still right.


Derron(Posted 2016) [#13]
If you use the "grid to screen"-functionality (or "screen-to-grid") some "more" times per loop (had to do this in my game "Apes Banana Conquest" - written in monkey) you might see some changes.
I try to use the float only when wanting a "percentage" (eg covering of a tile).

Of course it might be neglectable - and your thoughts on this are like I am thinking too (better doing some "securing" than later on running into trouble). So do not feel attacked, everything is okay.


bye
Ron


Bobysait(Posted 2016) [#14]

So do not feel attacked, everything is okay.


I didn't feel attacked at all :)
Actually I didn't know about the int/int being already a floor.


Derron(Posted 2016) [#15]
It is not directly a floor - it is just truncating the fractional part from the integer portion. For _me_ this is the same as "rounding down"/floor().


Interestingly this then truncates your function:
' relative position to Tile (in pixel)
Function convertPixelCoordToTilePixel(pixel_coord:Int, TileSize:Int, MapOffsetInPixel:Int)
	Return pixel_coord - ( Floor ( Float(pixel_coord-MapOffsetInPixel)/TileSize ) * TileSize )
End Function


to something really short:

' relative position to Tile (in pixel)
Function convertPixelCoordToTilePixel(pixel_coord:Int, TileSize:Int, MapOffsetInPixel:Int)
	'original
	'Return pixel_coord - ( Floor ( Float(pixel_coord-MapOffsetInPixel)/TileSize ) * TileSize )
	'step 1
	'Return pixel_coord - ( Floor ( (pixel_coord-MapOffsetInPixel)/TileSize ) * TileSize )
	'step 2
	'Return pixel_coord - ( (pixel_coord-MapOffsetInPixel)/TileSize * TileSize )
	'step 3
	'Return pixel_coord - ( pixel_coord-MapOffsetInPixel )
	'step 4
	'Return - ( -MapOffsetInPixel )
	'step 5
	Return MapOffsetInPixel
End Function


So if I have done everything correct, then this function is not doing what it is expected to do.

I think it should substract tile-dimensions from a given given pixel value. So isn't it a "modulo" ?

Return (pixel_coord - MapOffsetInPixel) Mod TileSize

-> make pixel_coord "absolute" (subtract your offset)
-> subtract the biggest amount of "TileSize*x" from that value
-> remainder is the pixels of the tile you are targeting


bye
Ron


Brucey(Posted 2016) [#16]
Maybe it's just me, but I still don't really know what he wants to calculate! :-p


Bobysait(Posted 2016) [#17]
It's not you, that's why I posted different functions instead of just the one who would fit ...

But I'm pretty sure he wants some earth coordinates ^_^


BlitzMan(Posted 2016) [#18]
Thanks for help guys

I was not taking the scrolling into consideration.In placing the player.

Thanks again