Jevi

Members
  • Content count

    16
  • Joined

  • Last visited

Community Reputation

131 Neutral

About Jevi

  • Rank
    Member
  1. I figured it out. Make sure you take into consideration where the point of origin is in a b2Body which is the center point of the b2Shape. I was not translating my images to the center and that's why they looked off.
  2. So i decided to add Box2D functionality to my little game engine as a component which can be added to an entity. As of right now the physics component, which is what actually has the b2Body associated with it and is getting updated by the b2World, is also updating the render component for the entity which is the results from the physics component are render to the screen (via the render component)   I am new to Box2D and it looks like I have everything correct but the collisions aren't being calculated correctly.   Here is the code which initializes and updates the render component from the physics component update results:     Here is a video of the collision sort of working: https://www.dropbox.com/s/ykm0camzs287jdu/Engine%202013-01-27%2015-46-41-63.avi   And here it is colliding on the other side and not working at all: https://www.dropbox.com/s/6dcqu6d3eofga8h/Engine%202013-01-27%2015-41-07-74.avi   Am I not converting/initializing something? void PhysicsComponent::Update(unsigned long dt) { if (!synched) { b2World* world = Engine::GetWorld(); b2BodyDef BodyDef; BodyDef.position.Set(entity->transform.p.x, entity->transform.p.y); BodyDef.angle = entity->transform.q.GetAngle(); switch (bodyType) { case 0: BodyDef.type = b2_staticBody; break; case 1: BodyDef.type = b2_dynamicBody; default: BodyDef.type = b2_dynamicBody; break; } body = world->CreateBody(&BodyDef); b2PolygonShape shape; int width = ((RenderComponent*) entity->GetComponent("render"))->GetSprite()->width; int height = ((RenderComponent*) entity->GetComponent("render"))->GetSprite()->height; float mwidth = EngineMath::PixelsToMeters((float) width) / 2.0f; float mheight = EngineMath::PixelsToMeters((float) height) / 2.0f; Debug::Log(Debug::LOG_DEBUG, "width: %f height: %f density: %f", mwidth, mheight, density); Debug::Log(Debug::LOG_DEBUG, "width: %i height: %i density: %f", width, height, density); shape.SetAsBox(mwidth, mheight); b2FixtureDef FixtureDef; FixtureDef.shape = &shape; FixtureDef.density = density; FixtureDef.friction = 0.5f; body->CreateFixture(&FixtureDef); synched = true; } entity->transform = body->GetTransform(); // Debug collision outline int width = ((RenderComponent*) entity->GetComponent("render"))->GetSprite()->width; int height = ((RenderComponent*) entity->GetComponent("render"))->GetSprite()->height; Graphics::DrawQuad(entity->transform.p.x, entity->transform.p.y, (float) width, (float) height, entity->transform.q.GetAngle(), 255, 0, 0, 255); //Graphics::DrawQuad(body->GetTransform().p.x, body->GetTransform().p.y, (float) width, (float)height, body->GetTransform().q.GetAngle(), 0, 255, 0, 255); }   world->Step(1.0f / 60.0f, 5, 5);
  3. This question has been answered here: http://stackoverflow.com/questions/14025740/convert-sdl-window-position-to-opengl-world-position
  4. I am trying to scale into a base image multiple times. So I should be able to from a base image which has no scale factor (or scale factor of 1) scale into it and then from that scaled image generated from the base image I should be able to scale into it again and again and again. My problem is that I do not know how to calculate what the x and y coordinate of the mouse should be when trying to select a region from a scaled image. Here are some images that will describe my situation.   *Note that all the red coordinates are what the x and y coordinates should be but the mouse x and y coordinates are always from 0,0 to 640,480 ( the display dimensions )   This is my base image. Note the stating x and y coordinates.   [attachment=12896:original.png]   This is the selection of the portion that will be scaled from the original x and y coordinates   [attachment=12897:originalzooming.png]   This is the result of that first selection scaled   [attachment=12899:zoomedin.png]   I want to be able to do another selection scale from this image but as I stated I don't know how to calculate what the x and y coordinates should translate to.   This is an example of what I should be able to do   [attachment=12898:zommedinzooming.png]   Note that I am also translating the image to the center of the selection so that the scaled in image will be centered in the display.   This is what I am currently doing to scale from the base image to a scaled image. *note that this does not work when trying to scale and already scaled image as I said because I need to calculate what the actual x and y coordinates of the mouse should be. The first 4 lines is where I presume the calculations of the actual x and y coordinates should be. // modify mouse coordinates to fit scaled image _mouseStart.x = _mouseStart.x; _mouseStart.y = _mouseStart.y; _mouseEnd.x = _mouseEnd.x; _mouseEnd.y = _mouseEnd.y; // get width and height of selection float ratio = (float) SCREEN_WIDTH / (float) SCREEN_HEIGHT; float newFracWidth = fabs(_mouseEnd.x - _mouseStart.x); float newFracHeight = newFracWidth / ratio; if (newFracWidth > 0 && newFracHeight > 0) { Vertex starts; Vertex ends; if (_mouseStart.x > _mouseEnd.x) { starts.x = _mouseStart.x - newFracWidth; starts.y = _mouseStart.y - newFracHeight; ends.x = _mouseStart.x; ends.y = _mouseStart.y; _fracNormalCenter.x = _mouseStart.x - (newFracWidth / 2); _fracNormalCenter.y = _mouseStart.y - (newFracHeight / 2); } else { starts.x = _mouseStart.x; starts.y = _mouseStart.y; ends.x = _mouseStart.x + newFracWidth; ends.y = _mouseStart.y + newFracHeight; _fracNormalCenter.x = _mouseStart.x + (newFracWidth / 2); _fracNormalCenter.y = _mouseStart.y + (newFracHeight / 2); } _portionStart.x = starts.x; _portionStart.y = starts.y; _portionEnd.x = ends.x; _portionEnd.y = ends.y; cout << "glOrtho Dimension: : " << _portionStart.x << " , " << _portionStart.y << " -- " << _portionEnd.x << " , " << _portionEnd.y << endl; starts = NormalToComplex(starts); ends = NormalToComplex(ends); _scaleFactor = (SCREEN_WIDTH / newFracWidth); // set new complex display dimensions _fracStart.x = starts.x; _fracStart.y = starts.y; _fracEnd.x = ends.x; _fracEnd.y = ends.y; _pointOffSet.x = (SCREEN_WIDTH / 2) - _fracNormalCenter.x; _pointOffSet.y = (SCREEN_HEIGHT / 2) - _fracNormalCenter.y;       This is how I set the new scaled image   glLoadIdentity(); glOrtho(_portionStart.x, _portionEnd.x, _portionEnd.y, _portionStart.y, 1, -1);
  5. [quote name='Jevi' timestamp='1355614950' post='5011098'] [quote name='Brother Bob' timestamp='1355612739' post='5011076'] What do you mean it won't get passed; what isn't passed to that function? Does it not generate a unique buffer handle? [/quote] I'm using Microsoft Visual Studio and it gives me the option to Break or Continue. This is what the message says "Unhandled exception at 0x77a215de in FractalGenerator.exe: 0xC0000005: Access violation." at that glGenBuffer line. Am I using the correct libraries for this? Im using glew for the extensions and sdl_opengl [/quote] I figured it out. I did not initialize glew functions. All I needed was glewInit(): and everything worked... thanks!
  6. [quote name='Brother Bob' timestamp='1355612739' post='5011076'] What do you mean it won't get passed; what isn't passed to that function? Does it not generate a unique buffer handle? [/quote] I'm using Microsoft Visual Studio and it gives me the option to Break or Continue. This is what the message says "Unhandled exception at 0x77a215de in FractalGenerator.exe: 0xC0000005: Access violation." at that glGenBuffer line. Am I using the correct libraries for this? Im using glew for the extensions and sdl_opengl
  7. [quote name='Brother Bob' timestamp='1355605046' post='5011034'] When drawing, you're binding the color buffer object but setting the vertex pointer. Sounds from your description though that you are much better of drawing pixel rectangle with glDrawPixels instead of drawing the individual pixels as points. [/quote] Yeah I have updated the code to use glColorPointer for the color buffer but that is not where my original problem is. Is there any reason why my code won't get passed glGenBuffers(1, &_vertexHandle);
  8. I have tried looking for the solution but I haven't found anything discussing the whole process. I'm trying to write my own fractal generator. What I do is I generate coordinates with a color associated with it which is loaded into vector<Point> _points. _points is then used to load the vector<float> _vertexBuffer and vector<float> _colorBuffer which are to be used to draw with the vbo. There might be a problem with the libraries that I'm linking. So suggestions on what opengl extension library I should use for vbos is welcomed. I have a feeling that I'm also not loading data in correctly from the buffers... any help is appreciated. Here is my code, I'm sure someone could notice the problem pretty easily: [CODE] #define NO_SDL_GLEXT #include <GL/glew.h> #include <SDL.h> #include <SDL_opengl.h> ... unsigned int FractalGenerator::_vertexHandle; unsigned int FractalGenerator::_colorHandle; vector<float> FractalGenerator::_vertexBuffer; vector<float> FractalGenerator::_colorBuffer; ... struct Color { float r, g, b; }; struct Point { float x, y; Color c; }; .... for (int i = 0; i < _points.size(); i++) { _vertexBuffer.push_back(_points[i].x); _vertexBuffer.push_back(_points[i].y); _colorBuffer.push_back(_points[i].c.r); _colorBuffer.push_back(_points[i].c.g); _colorBuffer.push_back(_points[i].c.b); } ... [/CODE] I'm getting a fault here, when I generate the the vbo for _vertexHandle [CODE] glGenBuffers(1, &_vertexHandle); glBindBuffer(GL_ARRAY_BUFFER, _vertexHandle); glBufferData(GL_ARRAY_BUFFER, _vertexBuffer.size() * sizeof(float), &_vertexBuffer.front(), GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glGenBuffers(1, &_colorHandle); glBindBuffer(GL_ARRAY_BUFFER, _colorHandle); glBufferData(GL_ARRAY_BUFFER, _colorBuffer.size() * sizeof(float), &_colorBuffer.front(), GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); ... glClear(GL_COLOR_BUFFER_BIT); glPushMatrix(); glScaled(_zoomFactor, _zoomFactor, 1); glTranslated(_xOffSet, _yOffSet, 0); glBindBuffer(GL_ARRAY_BUFFER, _vertexHandle); glVertexPointer(2, GL_FLOAT, 0, 0L); glBindBuffer(GL_ARRAY_BUFFER, _colorHandle); glColorPointer(3, GL_FLOAT, 0, 0L); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); glDrawArrays(GL_POINTS, 0, _vertexBuffer.size()); glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); glPopMatrix(); SDL_GL_SwapBuffers(); [/CODE]
  9. [quote name='Kaptein' timestamp='1351217940' post='4994012'] that's good to hear sorry i got it backwards, yes the order of operations is indeed backwards [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] this is how matrices work in general once you get a bit further into all this, and you are writing your own little matrix library (it's an important step to take) then you will see why ;) not to mention the possibilities [/quote] thanks again! getting about 1000 fps more with VBOs as compared to intermediate rendering even though its just a couple of lines on the screen I can already see how this is going to be crucial later on [img]http://public.gamedev.net//public/style_emoticons/default/laugh.png[/img]
  10. I got it I forgot the all this is done slightly backwards so here is the working code if anyone wants to know So I translate to the startx, starty then i rotate around it, I translate to -startx, -starty ( all that is done is g.rotate(..) ) then I translate to the current x,y coordinates of the object. As you can see in the following code those steps are implemented backwards in the code but are interpreted in the correct order by OGL. a little confusing but its not that bad [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]. Thanks for all the help guys!! [CODE] @Override public void render(Graphics g) { glLoadIdentity(); glPushMatrix(); glBegin(GL_POINTS); glVertex2f(x, y); glEnd(); glTranslatef(x - startx, y - starty, 0); g.rotate(startx, starty, angle); g.drawLines(); glPopMatrix(); } [/CODE]
  11. [quote name='Kaptein' timestamp='1351216682' post='4994004'] change glTranslatef(-startx, -starty, 0); to: glTranslatef(new_x - startx, new_y - starty, 0); [/quote] This does not work [CODE] @Override public void render(Graphics g) { glLoadIdentity(); glPushMatrix(); glBegin(GL_POINTS); glVertex2f(x, y); glEnd(); glTranslatef(startx, starty, 0); glRotatef(angle, 0, 0, 1); glTranslatef(x - startx, y - starty, 0); g.drawLines(); glPopMatrix(); } [/CODE]
  12. [quote name='Kaptein' timestamp='1351214814' post='4993997'] to do rotations, you have to translate the object to its rotational origin, called a PIVOT position, then rotate it there, and translate it back! if your object is a box created at (0,0) and all the way to (1,1), you have to translate it to (-0.5, -0.5) to rotate it around itself however, if your box was created from (-0.5, -0.5) to (0.5, 0.5) you can rotate it immediately, because its rotational origin is now (0, 0) from the start, if you wanted to rotate it around itself! 3ds max (or any other 3d CAD) is a great tool to visualize how this works to do the same thing with your model, you need to know its dimensions, and then its half-sizes, then translate it backwards so that the object is positioned with (0, 0) at the center of the object, then rotate it there (if that's what you wanted to do) from looking at the ship creation code from the link.. its hard for me to understand how exactly it is created, except to say i never would be doing it that way myself :S i'm sure theres some logic to it though! [/quote] Ok so now the rotation works correctly. I translate back to the starting x,y rotate it there but how do i move the ship to where the current x,y are? And yes my code may be a little confusing but its hard to stay true to object oriented programming when all the OGL tutorials I have found don't seem to code with that convention... [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img] This is my modified code [CODE] @Override public void render(Graphics g) { glLoadIdentity(); glPushMatrix(); glBegin(GL_POINTS); glVertex2f(x, y); glEnd(); glTranslatef(startx, starty, 0); glRotatef(angle, 0, 0, 1); glTranslatef(-startx, -starty, 0); g.drawLines(); glPopMatrix(); } [/CODE]
  13. [quote name='NumberXaero' timestamp='1351210725' post='4993972'] What happens if you try glRotatef(angle, 0, 0, 1); glTranslatef(x, y, 0); [/quote] That doesn't work either... I feel like if someone could see what was happening they could figure it out so he is a video of what is happening.. notice the dot witch is the x,y coordinate and then notice the ship https://www.dropbox.com/s/52mq14gj8b80mb4/Untitled.mp4
  14. [quote name='Solias' timestamp='1351209348' post='4993967'] Why are you doing the -x, -y translation? I assume the verts in the VBO are in object space already? [/quote] Because this is the method I used when I was using intermediate mode rendering. If I remove -x, -y translation the rotation error becomes even greater ( or less accurate ). This is what I do to create the lines and store them to memory [CODE] Line[] lines = new Line[] { new Line(x, y - height / 4, x + width / 3, y + height / 4), new Line(x + width / 3, y + height / 4, x, y), new Line(x, y, x - width / 3, y + height / 4), new Line(x - width / 3, y + height / 4, x, y - height / 4) }; vertexHandle = glGenBuffers(); vertexBuffer = BufferUtils.createFloatBuffer(lines.length * Line.numIndividualVertices); numCoordinates = Line.numCoordinates; numPairVertices = lines.length * numCoordinates; for (Line line : lines) { vertexBuffer.put(new float[] { line.getX1(), line.getY1(), line.getX2(), line.getY2() }); } vertexBuffer.flip(); glBindBuffer(GL_ARRAY_BUFFER, vertexHandle); glBufferData(GL_ARRAY_BUFFER, vertexBuffer, GL_DYNAMIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); [/CODE]
  15. I have a Ship object made up of 4 lines ( looks like the asteroids game ship ). I am rendering it with the vertex buffer object method. It gets drawn to the screen fine but when i go to rotate the ship the whole ship rotates around the (x,y) coordinate and not about the (x,y) coordinate, if that makes sense. CODE SNIPPETS - full code at: https://github.com/Jevi/Asteroids/tree/master/src/org/cstutorials This is how i render the ship object ( which is compromised of 4 lines loaded into a FloatBuffer ( im using LWJGL btw ) that FloatBuffer is loaded in to memory and I draw from that allocated memory. The random GL_POINT is to see where (x,y) currently is. [CODE] @Override public void render(Graphics g) { glLoadIdentity(); glPushMatrix(); glBegin(GL_POINTS); glVertex2f(x, y); glEnd(); g.rotate(x, y, angle); g.drawLines(); glPopMatrix(); } [/CODE] This is my drawLines() implementation, the variables seen inside are handled by a gameObjectManager which is not in these snippets ( no need ) [CODE] public void drawLines() { glEnableClientState(GL_VERTEX_ARRAY); glBindBuffer(GL_ARRAY_BUFFER, currentVertexHandle); glVertexPointer(numCoordinates, GL_FLOAT, 0, 0L); glDrawArrays(GL_LINES, 0, numPairVertices); glBindBuffer(GL_ARRAY_BUFFER, 0); glDisableClientState(GL_VERTEX_ARRAY); } [/CODE] Finally here is my rotatation implementation, it use to work fine when I used immediate mode rendering but now that I have VBOs it does not work the same [CODE] public void rotate(float x, float y, float angle) { glTranslatef(x, y, 0); glRotatef(angle, 0, 0, 1); glTranslatef(-x, -y, 0); } [/CODE]