Vector shapes

Member
Posts: 131
Joined: 2010.08
Post: #1
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?
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #2
A number of people use SVG programs because it's a relatively easy format to parse.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Member
Posts: 131
Joined: 2010.08
Post: #3
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?
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #4
What is the problem exactly?

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

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Member
Posts: 131
Joined: 2010.08
Post: #5
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?
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #6
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.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #7
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();
}
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #8
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();

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Member
Posts: 131
Joined: 2010.08
Post: #9
(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?
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #10
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.
Quote this message in a reply
Member
Posts: 131
Joined: 2010.08
Post: #11
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.
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #12
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];
}
Quote this message in a reply
Member
Posts: 131
Joined: 2010.08
Post: #13
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?
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #14
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.
Quote this message in a reply
Member
Posts: 131
Joined: 2010.08
Post: #15
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?
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Reflect a vector in relation to another vector ipeku 4 4,997 Dec 12, 2011 07:17 AM
Last Post: ipeku
  Origin problems - rotating shapes frances_farmer 1 2,382 Feb 10, 2007 08:08 AM
Last Post: unknown
  Rendering 2D Shadows for Convex Shapes Nick 16 9,294 Nov 28, 2006 11:50 AM
Last Post: memon