Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 15 Jul 2012
Offline Last Active Jan 28 2013 09:46 PM

Topics I've Started

Box2D Incorrect Collision Calculation

27 January 2013 - 03:48 PM

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;
			case 1:
				BodyDef.type = b2_dynamicBody;
				BodyDef.type = b2_dynamicBody;
		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;
		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);

OpenGL 2D Selection Scaling Multiple Times

23 December 2012 - 05:58 PM

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.


Attached File  original.png   32.35KB   57 downloads


This is the selection of the portion that will be scaled from the original x and y coordinates


Attached File  originalzooming.png   33.63KB   61 downloads


This is the result of that first selection scaled


Attached File  zoomedin.png   34.52KB   57 downloads


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


Attached File  zommedinzooming.png   43.72KB   55 downloads


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);
						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


	glOrtho(_portionStart.x, _portionEnd.x, _portionEnd.y, _portionStart.y, 1, -1);

Using std::vector to load data to VBO

15 December 2012 - 02:13 PM

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:

#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++)


I'm getting a fault here, when I generate the the vbo for _vertexHandle

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);


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);
glDrawArrays(GL_POINTS, 0, _vertexBuffer.size());

Quick help needed with 2d rotation of vertex buffer object OpenGL

25 October 2012 - 04:55 PM

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.

public void render(Graphics g)
  glVertex2f(x, y);
  g.rotate(x, y, angle);

This is my drawLines() implementation, the variables seen inside are handled by a gameObjectManager which is not in these snippets ( no need )

public void drawLines()
  glBindBuffer(GL_ARRAY_BUFFER, currentVertexHandle);
  glVertexPointer(numCoordinates, GL_FLOAT, 0, 0L);
  glDrawArrays(GL_LINES, 0, numPairVertices);
  glBindBuffer(GL_ARRAY_BUFFER, 0);

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

public void rotate(float x, float y, float angle)
  glTranslatef(x, y, 0);
  glRotatef(angle, 0, 0, 1);
  glTranslatef(-x, -y, 0);

2D TTF Text Rendering with SDL & OpenGL Issues

15 July 2012 - 08:19 PM

So I have looked around and I haven't found the best way to actually implement this in SDL & OpenGL. At this moment I can display text to the screen with a TTF but its not really displaying the text the way it should be. Attached is a screen shot of how the engine looks like when displaying text. Basically from what I can see in my code, I think what is happening is that I am not blitting my text texture with the my main SDL_Surface that I am using as my window. I say that because in the picture my character who has a red collision box around him is being covered up my the text texture I'm rendering my text with. Any ideas of what I can do?

All the engine code can be found here: https://github.com/Jevi/SDL_GL_ENGINE

In my game loop I call beginDraw() and endDraw() before and after I draw everything to the screen.

void graphics::SDL_GL_RenderText(float x1, float y1, int width, int height, const char* text, int ptsize, const char* ttfLoc, int r, int g, int b){
SDL_Surface* temp;
SDL_Surface* temp2;
SDL_Rect rect;
TTF_Font* font;
SDL_Color textColor;
unsigned int texture;
font = TTF_OpenFont( ttfLoc , ptsize );
textColor.r = r;
textColor.g = g;
textColor.b = b;
temp = TTF_RenderText_Blended( font, text, textColor );
// width = nextpoweroftwo(width);
// height = nextpoweroftwo(height);
temp2 = SDL_CreateRGBSurface(0, width, height, 32, r, g, b, 0);
SDL_BlitSurface(temp, 0, temp2, 0);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, temp2->w, temp2->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, temp2->pixels);
/* prepare to render our texture */
glBindTexture(GL_TEXTURE_2D, texture);
glColor3f(1.0f, 1.0f, 1.0f);
/* Draw a quad at location */
glVertex2f(x1, y1);
glVertex2f(x1 + temp2->w, y1);
glVertex2f(x1 + temp2->w, y1 + temp2->h);
glVertex2f(x1, y1 + temp2->h);
glDeleteTextures(1, &texture);

void graphics::GL_BeginDraw(){
glPushMatrix(); //Start rendering phase
glOrtho(0,width,height,0,-1,1); //Set the matrix

void graphics::GL_EndDraw(){
glPopMatrix(); //End rendering phase

P.S: I'm going to make another post for this but I might as well ask this also if you guys are already looking at my code. I have been noticing some memory issues with the engine. In task manager the memory allocated to my program is constantly climbing since its starts at around 11000 K. Help with that would also be appreciated. Thanks