gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > Get current 3D coordinate ?

#147544 - ix78 - Sat Dec 22, 2007 2:14 pm

Hi all,

I'm writing a small game in 3D (breakout) and have already bumped into a problem >_<"

Code:

float obj_x;
float obj_y;
float obj_origin_x;
float obj_origin_y;
float obj_angel;
float obj_travel;

void ball_fire(void)
{
 obj_origin_x = obj_x;
 obj_origin_y = obj_y;
 obj_angel = 30;
 obj_travel = 0;
}

void ball_update(void)
{
 glPushMatrix();
 glTranslatef(obj_origin_x, obj_origin_y, 0);
 glRotateZ(obj_angel);
 glTranslatef(0, obj_travel, 0);
 DrawObj();
 glPopMatrix(1);

 obj_travel += .1;
}


The code above should be pretty straightforward...

The problem now is I do not know the ball's actual x, y coordinate in relation to the original world coordinate for me to do collision detection with other objects in the world.

Is there a way to get access to current world matrix? Or do I have to write my own matrix functions to calculate the position?

Hope I'm not confusing anybody here :P

#147552 - Sausage Boy - Sat Dec 22, 2007 11:14 pm

It sounds like you should learn some trigonometry. I found this guide pretty helpful:

http://www.clarku.edu/~djoyce/trig/

Your current strategy, while cool, is a very unusual and impractical way to handle this problem. But yeah, come back after you've really studied, and really _understand_ trigonometry, especially sines and cosines. They will help you greatly in the long run.
_________________
"no offense, but this is the gayest game ever"

#147561 - crossraleigh - Sun Dec 23, 2007 7:20 am

If you already understand trigonometry and you still want the matrix, there is a memory area that GBATEK calls CLIPMTX_RESULT which libnds will read for you using glGetFixed(GL_GET_MATRIX_POSITION, &matrix).

#147583 - M3d10n - Sun Dec 23, 2007 10:41 pm

Reading the matrix for this sounds really overkill. You are using the graphics functions to move the ball position and trying to read it back. This is not the best way to do it.

You need proper variables for tracking the ball's position (ball_x, ball_y). Update your ball position (by modifying ball_x and ball_y, as example), then pass it to glTranslate the render your ball in the desired position instead.

--EDIT--

Ah, now I understood why you want to read the matrix: you're using an angle-based rotation to change the ball direction. Like Sausage Boy, trigonometry allows you to calculate the coordinates the ball should move for a given angle, but for a simple breakout game you don't even need to worry about angles:

Use variables to keep track of the ball's X and Y velocities. On every update, add the X velocity to the balls X position and the Y velocity to the ball's Y position.

Then you only need to invert the sign of the X velocity if the ball collides with an block horizontally, and the Y velocity's sign if it hits something vertically. To change the angle when the ball hits the paddle, use some number fudging or hardcoded X and Y velocities based on where in the paddle the ball hit: a greater Y velocity than an X one if the ball hits the middle of the paddle, and the inverse if it hits the sides.