iDevGames Forums
Vector shapes - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: Vector shapes (/thread-8123.html)

Pages: 1 2


Vector shapes - Miglu - Sep 21, 2010 07:52 AM

What is the best application to make vector shapes so that the vectors can be used in Chipmunk to do collision detection with the shapes?


RE: Vector shapes - Skorche - Sep 21, 2010 08:13 AM

A number of people use SVG programs because it's a relatively easy format to parse.


RE: Vector shapes - Miglu - Sep 21, 2010 08:36 AM

Thanks. I downloaded Inkscape.
This is an offtopic question:
Code:
- (void) drawRect:(NSRect)rect {
    [self reshape];
    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glPushMatrix();
    glLoadIdentity();
    glOrtho(0.0, viewAspect, 0.0, 1.0, -1.0, 1.0);
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();
    glPushMatrix();
    [block drawBlock];
    glPopMatrix();
    glPushMatrix();
    [ball drawBall];
    glPopMatrix();
    glPopMatrix();
    glMatrixMode(GL_PROJECTION);
    glTranslatef(cpBodyGetPos(ball.body).x/size.height, cpBodyGetPos(ball.body1).y/size.height, 0.0);
    glPopMatrix();
    [[self openGLContext] flushBuffer];
}
The projection does not follow the ball even though I have glTranslatef(cpBodyGetPos(ball.body).x/size.height, cpBodyGetPos(ball.body).y/size.height, 0.0). What is the problem?


RE: Vector shapes - Skorche - Sep 21, 2010 08:48 AM

What is the problem exactly?

Did you mean: cpBodyGetPos(ball.body).x/size.width


RE: Vector shapes - Miglu - Sep 21, 2010 09:00 AM

I want the "camera" to follow the ball.
viewAspect = size.width / size.height, so size.height should be the denominator. In any case, the glTranslatef has no effect. What is the problem?


RE: Vector shapes - Skorche - Sep 21, 2010 10:50 AM

I guess I didn't read it very closely the first time

Three problems:
1) You are setting the translation after drawing your object.
2) You are also immediately poping the translation before using it for anything else.
3) You have waaay more push/pops in there than you need. Twice you are pushing the identity matrix onto the stack. This is basically a no-op that wastes the very small amount of stack space you have available. You should also check that you aren't overflowing the projection matrix stack, as it usually is only 2 deep.


RE: Vector shapes - AnotherJake - Sep 21, 2010 11:23 AM

Balancing pushes and pops can be very confusing. What I like to do is make sure all my code in between a push and pop is indented one level, like this pseudocode:
Code:
void myFunc(void)
{
    glPushMatrix();
        glLoadIdentity();
        glTranslatef(21.0f, 0.0f, 0.0f);
        myDrawSomething();
        glPushMatrix();
            glTranslatef(0.0f, 13.0f, 1.0f);
            glRotatef(45.0f, 1.0f, 0.0f, 0.0f);
            myDrawSomethingElse();
            glPushMatrix();
                glTranslate(300.0f, 0.0f, 0.0f);
                myDrawSomething();
            glPopMatrix();
        glPopMatrix();
    glPopMatrix();
}



RE: Vector shapes - Skorche - Sep 21, 2010 12:39 PM

I take that one step further even and add {} around them. It's more friendly with auto-formatting, and I generally create a bunch of throwaway locals anyway.

Code:
glPushMatrix(); {
    ...
} glPopMatrix();



RE: Vector shapes - Miglu - Sep 21, 2010 01:08 PM

(Sep 21, 2010 10:50 AM)Skorche Wrote:  1) You are setting the translation after drawing your object.
2) You are also immediately poping the translation before using it for anything else.
3) You have waaay more push/pops in there than you need. Twice you are pushing the identity matrix onto the stack. This is basically a no-op that wastes the very small amount of stack space you have available. You should also check that you aren't overflowing the projection matrix stack, as it usually is only 2 deep.

1. and 2. It is the projection matrix that I want to modify. Can it not be done after the modelview is done?
3. I removed the glLoadIdentitys. Which pushes and pops are not needed?

I tried to put the glTranslatef at the start, before the drawing. Now the whole modelview bounces as the ball bounces, even though the glTranslatef is modifying the projection and not the modelview matrix, and the "camera" still does not follow the ball. How to correct this problem?


RE: Vector shapes - AnotherJake - Sep 21, 2010 01:17 PM

1) Think about this for a second... The matrix transform of the vertices is done at the time you draw the object, not afterwards.
2) I don't think you understand how this works. You push a matrix onto the matrix stack, which copies the current one. Then you modify that matrix. Then that matrix is used to transform whatever you need transformed. Then you pop it off the stack and it goes back to what was there before it. So if you push a matrix, transform it, then pop it, then nothing happens. At all. Except wasting CPU cycles.
3) PushMatrix, LoadIdentity, PushMatrix is totally a waste. When you LoadIdentity, you are setting the current matrix on the stack to the identity matrix. Why push another matrix onto the stack right after that? It makes no sense. Just PushMatrix, then LoadIdentity, and then do whatever you want right there without pushing another matrix. Look at my pseudocode for an example.

You *really* need to go study up on matrix transforms and how to use them. This is important stuff, and you'll only face extreme frustration without fully understanding it before you move along.
Try this matrix tutorial.


RE: Vector shapes - Miglu - Sep 21, 2010 01:39 PM

Alright, now I understand. However, the problem is not solved yet.
Code:
- (void) drawRect:(NSRect)rect
{
    [self reshape];
    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, viewAspect, 0.0, 1.0, -1.0, 1.0);
    glTranslatef(cpBodyGetPos(ball.body).x/size.height, cpBodyGetPos(ball.body).y/size.height, 0.0);
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
        [block drawBlock];
    glPopMatrix();
    glPushMatrix();
        [ball drawBall];
    glPopMatrix();
    [[self openGLContext] flushBuffer];
}
The whole modelview bounces as the ball bounces, and the "camera" still does not follow the ball.


RE: Vector shapes - AnotherJake - Sep 21, 2010 01:48 PM

One major problem in there is that it looks like you're trying to modify your projection matrix to use that as your camera. Don't do that. What you do instead is transform your modelview matrix for your "camera" by transforming the scene opposite of what you want your camera to do. You need to, in effect, move the entire scene around the camera, not move the camera in the scene. Get it?

Something like this:
Code:
- (void) drawRect:(NSRect)rect
{
    [self reshape];
    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, viewAspect, 0.0, 1.0, -1.0, 1.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(-(cpBodyGetPos(ball.body1).x/size.height), -(cpBodyGetPos(ball.body1).y/size.height), 0.0);
    glPushMatrix();
        [self blockDraw];
    glPopMatrix();
    glPushMatrix();
        [ball ball];
    glPopMatrix();
    [[self openGLContext] flushBuffer];
}



RE: Vector shapes - Miglu - Sep 21, 2010 01:59 PM

What I am trying to do is this: I want the ball to stay in the view's center even though it moves.

Could you answer two questions:
1. Why is it better to modify the modelview?
2. I did what you suggested, but the whole modelview is still bouncing along with the ball, and it happens below the view if I negate the x and y, so it can not be seen. How to solve this problem?


RE: Vector shapes - AnotherJake - Sep 21, 2010 02:10 PM

1) I shall refer you to the OpenGL FAQ: 8.030 Where should my camera go, the ModelView or Projection matrix?
2) I'm not smart enough to figure out your math for you on this one (especially because my mind is busy cranking away on a problem of my own right now). You're setting your projection to normalized coordinates (i.e. 0.0 to 1.0), is your physics world using those same coordinates? Skorche can probably help you better on this.


RE: Vector shapes - Miglu - Sep 21, 2010 02:39 PM

I think that glTranslatef(cpBodyGetPos(ball.body).x/size.height - 0.5*viewAspect, cpBodyGetPos(ball.body1).y/size.height - 0.5, 0.0) is actually needed to centralize the ball. However, it does not work. Anyone who has done a basic racing or platform game knows the answer to this problem, as the car or character is usually centralized. What is the answer?