Weird coordinate problem

Blitz3D Forums/Blitz3D Programming/Weird coordinate problem

Rimmsy(Posted 2004) [#1]
Hey everyone, I'm having a little problem working out how to do the following, and hope someone can help. My brain doesn't seem to want to grasp it.

I'm making a 2D top down space game (in blitz3d), where the player is always centred in the screen. First I create a planet at certain coords, say -100,-100. This puts it above and to the left of you when you begin at 0,0.

Now, when I move I use velocity_x# and velocity_y# variables to track my speed and move all the planets accordingly (see simple code below). Therefore, when I move left 100 units, the planet mentioned above will be at 0,-100. All good. I also keep a set of variables handy (space_x and space_y) which my velocity variables are always added to. This gives me a sort of global position. erm, maybe.

Now, my problem comes when I want to position my ship somewhere else. Let's say I want to position it at the planet above immediately. How would I go about doing it?

moving the planets
for p.planet=each planet
  p\x=p\x+velocity_x
  p\y=p\y+velocity_y
next


Is this the best way to do it, or am I overlooking something very simple?


Perturbatio(Posted 2004) [#2]
would you not be better moving the viewport rather than all the objects?
this way you can jump anywhere you want easily by just repositioning the viewport.

*EDIT*

I think I've just lost the plot actually...


Rimmsy(Posted 2004) [#3]
Mmmm, it's frazzled my brain. There has to be a better method of moving around the galaxy other than moving everything else. it'd make multiplayer a lot easier, I just can't think of anything. You can see some screenshots on my site under space war.


fredborg(Posted 2004) [#4]
Make an OffsetX and OffsetY variable instead, and use them only when drawing your stuff.

Something like this, maybe?
Graphics 640,480,0,2
SetBuffer BackBuffer()

Type player
	Field x,y
End Type

Type planet
	Field x,y
End Type

OffsetX = 320
OffsetY = 240

pl.player = New player
pl\x = OffsetX
pl\y = OffsetY

For i = 0 To 100
	p.planet = New planet
	p\x = Rand(-200,200)
	p\y = Rand(-200,200)
Next

Repeat

	Cls

	pl\y = pl\y+(KeyDown(208)-KeyDown(200))
	pl\x = pl\x+(KeyDown(205)-KeyDown(203))
	
	If pl\x<100
		offsetX = offsetX+(100-pl\x)
		pl\x = 100
	EndIf
	If pl\x>540
		offsetX = offsetX+(540-pl\x)
		pl\x = 540
	EndIf

	If pl\y<100
		offsetY = offsetY+(100-pl\y)
		pl\y = 100
	EndIf
	If pl\y>380
		offsetY = offsetY+(380-pl\y)
		pl\Y = 380
	EndIf
		
	Color 255,0,0
	Rect pl\x,pl\y,3,3,True

	Color 255,255,255
	For p.planet = Each planet
		Oval p\x+OffsetX,p\y+OffsetY,5,5,True
	Next

	Flip

Until KeyHit(1)
End



Wiebo(Posted 2004) [#5]
I would just attach the camera to the player, and move the player, that way you won't have to move everyting... Well, only the player, ofcourse :)


Rimmsy(Posted 2004) [#6]
*nods appreciatively* Thanks Freddy, this solves all my problems.

edit: weibo, it's draw completly 2d but I do use the spriteControl lib in the archives to get sun glare, so there isn't really a camera. But freddy's solution solves it pretty much. Oh, now I've got to re-write chunks of code. Tell yourself it'll be worth it in the end.


Rimmsy(Posted 2004) [#7]
Marvellous! I'd already written it quite modular, so it wasn't difficult to change the bits I wanted. Thanks again Fredborg, you're a lifesaver. Not literally, that'd be silly.


fredborg(Posted 2004) [#8]
No problem, Rims :)