ARGHH I suck at math!! Help!!!

BlitzMax Forums/BlitzMax Programming/ARGHH I suck at math!! Help!!!

Craig H. Nisbet(Posted 2007) [#1]
I'm trying to make and asteroids game as my first Max project and I having an issue that would be super easy in 3D, but since I'm using 2d, I have to create the effect I want with math.

I'm trying to make an asteroids game that instead of the player ship rotating around and flying around the screen, the player ship stays stationary in the middle of the screen facing up and when the player turns the entire map, or at least the bad guys positions, rotates around him as if the world if turning relative to him, instead of vise versa. This would be all top down 2d.

I guess the closest thing I can relate to this is a top down radar in a 3d game. the player is in the middle and the bad guy blips rotate around the player relative to the direction he's face and where he's moving.

What I'm looking for is a way to plot the bad guy new transformed position relative to the player ship in 2d, so if a bad guy is in front of the player at the top of the sceen and the player turns left, all bad guys will rotate right around the player.

I guess what I'm trying to do is to transform the world space with all the bad guy ships in to the local space of the player ship.

Hope this all makes sense. Got any ideas?


Thareh(Posted 2007) [#2]
I've never tried to do this myself, But I guess that you could position everything based from the middle. eg:

SetOrigin( GraphicsWidth()/2, Graphicsheight()/2 )

And then use SetRotation to rotate everything, except the player ship.

Just an idea :)


Craig H. Nisbet(Posted 2007) [#3]
Yeah, unfortunately, that won't work for what I need.


altitudems(Posted 2007) [#4]
Hrrmmm...
Would 2D Camera help? Sounds like you should seperate world coords from screen ones.
I might have some code lying around. It doesn't support rotation though. I'll try and find it.


tonyg(Posted 2007) [#5]
this might help.


ImaginaryHuman(Posted 2007) [#6]
You would need nested calls to setting rotation, since each time you call it is resets any previous rotations. You need to be able to rotate the game world (the modelview matrix ideally) while also allowing individual rotations per object.

Alternatively you could set the handle for each alien ship or asteroid to be centered on the player so that when there is any rotation performed it rotates around the player.


altitudems(Posted 2007) [#7]
Here is some code that might help, except for the matrix rotation issue. I'll let someone smarter than I help there.

Camera mod:


Quick example:



Jesse(Posted 2007) [#8]
see if this helps not thourougly tested, optimized or as sophisticated but hopefully you'll get the idea:



sswift(Posted 2007) [#9]
My sprite system would allow you to do this. You would just create a pivot in the center of the screen, and attach every sprite to it, except the player ship. Then you turn the pivot instead of the ship, and everything attached to it would rotate around the pivot.

Allowing the ship to move through this rotated space is a bit more difficult. I'm sure the system can handle it, I'm just having a hard time visualizing how you would need to move and rotate that pivot. I think all you would need to do is move the pivot in the opposite way that you want the ship to move, so turning the ship to the left would equal a pivot rotation to the right, and moving the ship upwards, would result in the pivot moving downwards. Hm... no that's not right. Cause when the pivot is no longer centered on the ship, if you then turn the ship, and the pivot turns, then the ship's movement in pivot space would be an arc, instead of turning in place...

Maybe you would need to create two pivots. One stays centered in the middle of the screen, and rotates, and the other is attached to that, and moves, and everything else is attached to the moving one.

Or maybe you could detach and reattach everything to the pivot each frame to calculate the rotation and movement seperately and leave them where they end up on detachment.


FlameDuck(Posted 2007) [#10]
Here is a copy of our current 2DVector class pulled straight from svn:

You might want to comment out the dependencies. I believe the function you're most interested in is the rotateAbout() method.


big10p(Posted 2007) [#11]
I don't have bmax so there may be some trickery I'm not aware of, but I'd just do something like:

- Move and set each game objects position and rotation in the game logic as normal, just as if the gameworld was axis-aligned, ala Asteroids.

- At render time, or just before, transform all game objects to the rotated world as follows (pseudo code):
cos_rot# = Cos(-90 - player.rot)
sin_rot# = Sin(-90 - player.rot)

origin_x = screen_width / 2
origin_y = screen_height / 2

For this = Each game_object

	; Get vector from player to this game object.
	x = this.x - player.x
	y = this.y - player.y

	; Transform (rotate) vector.
	trans_x = (x * cos_rot) - (y * sin_rot)
	trans_y = (y * cos_rot) + (x * sin_rot)
	
	; Set actual position this object needs to be drawn at.
	this.screen_x = origin_x + trans_x
	this.screen_y = origin_y + trans_y
	
	; Set actual rotation this object needs to be rotated to.
	this.screen_rot = this.rot + player.rot

Next

(Not tested, but you should get the idea)

[edit]Ooops. Forgot something.