offsetting objects

BlitzMax Forums/BlitzMax Programming/offsetting objects

This is probably a stupid question, but I am really terrible with math and haven't been able to figure this out. I am writing a side scrolling game that needs to offset objects with scrolling. When a player shoots a bullet that uses sin/cos for an angled shot, how would I go about moving this bullet backwards as the player runs forward? I've tried changing the X, Y origin, but this changes the whole path of the bullet.

Here is example code (not bmax because im lazy)
Graphics 640, 480, 16, 2
SetBuffer BackBuffer()

A = 290: R = 0
X = 320: Y = 240

While Not KeyDown(1)

Rect R * Cos(A) + X, R * Sin(A) + Y, 4, 4
R = R + 3
;X = X - 1

Flip:Cls
Wend:End
If you uncomment the X line, the bullet path changes instead of moving backwards while still traveling at the same angle. How do I accomplish this?

shouldn't changing the origin (as to where you are in the world, and what you draw) handle this?

what I mean is that both the player and the bullet has it's own x,y coordinate which are updated each frame (or so).
you just let them do their own calculations, then when you're drawing the screen, you center the screen around the player coord with the origin command(or some other word coordinate, if you use that)..

* edit *
Trying now, I'll let you know how it goes.

The problem is, my player position doesn't really change once scrolling begins, it stays centered on screen and the map moves around him, so each bullet fired will start at the same position. Is there an easier way to do this than setting an origin for both the bullet and the player?

Please help me out here, I'm still stuck on this. When the player reaches the center on the screen (the point where map scrolling begins), how can I have the bullet move backward as you move forward, and still keep the player centered?

Graphics 640, 480, 16, 2
SetBuffer BackBuffer()

A = 290: R = 0

PX = 20: PY = 260
While Not KeyDown(1)

Origin X, Y
Rect R * Cos(A), R * Sin(A), 4, 4
R = R + 3

Origin PX, PY
Rect 0, 0, 4, 4

If KeyDown(205) Then
	If PX < 320 Then PX = PX + 1 Else PX = 320
EndIf

If KeyDown(203) Then PX = PX - 1

If KeyHit(57) Then A = 290: R = 0: X = PX + OX: Y = PY

Flip:Cls
Wend:End
Use left/right to move and space to shoot.

I don't know what you're asking. The result looks alright.

OK, I'll try to explain better.

When the player reaches the center of the screen, map scrolling begins. This means the player appears to be moving forward, even though he is still at the same position (320).

In the current code example, the bullet originates from the center point of the screen and remains there, even when you continue to hold right. My question is, once centered, how do I get the bullet to move backward as you hold right, while still keeping the player at the same screen position.

This is a quick hack:
Man, I am *not* used to blitz3d again after using bmx for a long while. This should do what you want. You're bascally offsetting the drawing not the position. See the code.

Graphics 640, 480, 16, 2
SetBuffer BackBuffer()

Global offsetX,offsetY


Type bullet
	Field x,y,angle
End Type
Function updateBullets()
	For i.bullet=Each bullet
		i\x=i\x+Cos(i\angle)*1 ; they don't get updated with the offset, only drawn
		i\y=i\y+Sin(i\angle)*1
	Next	
End Function
Function drawBullets()
	Color 255,0,0
	For i.bullet=Each bullet		
		Rect i\x+offsetX,i\y+offsetY,2,2 ; we draw them with the offset.	
	Next
	Color 255,255,255
End Function

Type backgroundItem
	Field x,y
End Type
Function drawBackgroundItem()
	For i.backgroundItem=Each backgroundItem
		Rect i\x+offsetX,i\y+offsetY,20,20 ; we draw them with the offset.
	Next
End Function


A = 290: R = 0
PX = GraphicsWidth()/2
PY = 260

For i=1 To 17
	Local u.backgroundItem=New backgroundItem
	u\x=Rand(-GraphicsWidth(),GraphicsWidth()*2)
	u\y=Rand(0,GraphicsHeight())
Next


While Not KeyDown(1)

	drawBackgroundItem()

	updateBullets()
	drawBullets()	

	Rect px, py, 4, 4	
		
	If KeyDown(205) Then offsetX=offsetX-1	
	If KeyDown(203) Then offsetX=offsetX+1
	
	If KeyHit(57)
		Local j.bullet=New bullet
		j\x=px-offsetX
		j\y=py-offsetY
		j\angle=Rand(360)
	EndIf

	Text 0,0,"offsetX:"+offsetX
	Text 0,20,"offsetY:"+offsetY

Flip:Cls
Wend:End


Awesome. Thanks much, Rimmsy.

No problem. That code ^^^ is a bit of a shoddy version but you should be able to get the gist of it.