Sign in to follow this  
c-gibson-s

Interesting positioning issues..

Recommended Posts

Hey..i'm having trouble with a small shooter thing i am working on. I'm using OpenGL w/ SDL, and VC.NET 2003. The problem is when you click the ship is supposed to shoot off a bullet at a given location, then that bullet, according to the given velocity, is supposed to travel. Simple. Not so much. I'm not seeing any bullets. So i debugged some stuff, and came out with something interesting. I outputted the ship position, the bullet position, and the bullet velocity, all when the bullet is fired. The numbers vary slightly depending on where the ship is, but for sake of example, here is what i got. Ship position - X: 0.02000000 Y: -3.8000000 Z: 25.00000 (the Z never changes, 2d :) ) Bullet position - X: -0.002654 Y: -0.002654, Z: -0.002654 (something is really up with that, the bullet.z is quite literally bullet.z = ship.z) Bullet velocity: -0.006635 (on the y axis) In the code (below), i made it so the bullet should fire just above and to the right of the ship X,Y..something like bullet.pos.x = ship.pos.x + 0.3..etc...so, for the first bullet position, with my given values, the bullet should have been at like X: 0.320000 Y: -4.something, and Z: 25.0000 (never changes, or shouldnt)..but as you saw earlier, the bullet position is completely and utterly way off. So yeah, any ideas? There are no calculations being done between when the ship position and bullet position is being outputted to the stderr file, besides the + 0.3 or 0.8. Here is the class code, more or less. jetHero.cpp
[source lang=cpp]
#include "jetHero.h"

#include <windows.h>
#include <gl/gl.h>
#include <gl/glpng.h>

#include "jetGame.h"
#include "jetSDL.h"
#include "jetHeroAmmo.h"

jetHero::jetHero()
{
	game = jetGame::getInstance();

	pos[0] = 0.0;
	pos[1] = -3.0f;
	pos[2] = 25.0f;

	size[0] = 0.7;
	size[1] = 0.85;

	bound[0][0] = -10.0;
	bound[0][1] =  10.0;
	bound[1][0] = -7.5;
	bound[1][1] =  7.5;

	m_show = true;

	loadTex();
}

jetHero::~jetHero()
{
	delTex();
}

void jetHero::loadTex()
{
	pngInfo tmpi;

 	maintex = pngBind("png/hero.png", PNG_NOMIPMAPS, PNG_ALPHA, &tmpi, GL_CLAMP, GL_LINEAR, GL_LINEAR );
}

void jetHero::delTex()
{
	glDeleteTextures( 1, &maintex );
	maintex = 0;
}

void jetHero::update()
{
	
}

void jetHero::fireGun()
{
	float p[3];
	p[2] = pos[2];

	p[0] = pos[0]+0.3;
	p[1] = pos[1]+0.8;
	game->heroAmmo->addAmmo( p );

}

void jetHero::moveEvent(int x, int y)
{
	pos[0] += x * 0.03;
	pos[1] += -y * 0.03;

	if		(pos[0] < bound[0][0])	pos[0] = bound[0][0];
	else if	(pos[0] > bound[0][1])	pos[0] = bound[0][1];
	if		(pos[1] < bound[1][0])	pos[1] = bound[1][0];
	else if	(pos[1] > bound[1][1])	pos[1] = bound[1][1];

}

void jetHero::drawGL()
{
	glPushMatrix();

		glTranslatef( pos[0], pos[1], pos[2] );

		if( m_show )
		{
			glColor4f( 1.0, 1.0, 1.0, 1.0 );
			glBindTexture( GL_TEXTURE_2D, maintex );
			game->drawQuad( size[0], size[1] );
		}

	glPopMatrix();


}


jetHeroAmmo.cpp
#include "jetHeroAmmo.h"

jetHeroAmmo::jetHeroAmmo()
{
	game = jetGame::getInstance();

	ammoRoot = new jetAmmo();
	ammoSize[0] = 1.0;
	ammoSize[0] = 1.0;

	ammoDamage = 3;

	ammoPool = new jetAmmo();

	loadTex();
}

jetHeroAmmo::~jetHeroAmmo()
{
	jetAmmo *cur;
	jetAmmo *del;

	clear();

	cur = ammoPool->next;

	while(cur)
	{
		del = cur;
		cur = cur->next;
		delete del;
	}

	delete ammoPool;
	delete ammoRoot;

	delTex();
}


void jetHeroAmmo::loadTex()
{
	pngInfo tmp;

	ammoTex = pngBind("png/herobullet.png", PNG_NOMIPMAPS, PNG_ALPHA, &tmp, GL_CLAMP, GL_LINEAR, GL_LINEAR );

	fprintf( stderr, "W: %d, H: %d \n", tmp.Width, tmp.Height );
}

void jetHeroAmmo::delTex()
{
	glDeleteTextures( 1, &ammoTex );
	ammoTex = 0;
}

jetAmmo *jetHeroAmmo::getNew()
{
	jetAmmo *returnAmmo;

	if( ammoPool->next )
	{
		returnAmmo = ammoPool->next;
		ammoPool->next = returnAmmo->next;
	}
	else
	{
		returnAmmo = new jetAmmo();
	}

	return returnAmmo;
}

void jetHeroAmmo::dieAmmoDie( jetAmmo *dead )
{
	dead->back = 0;
	dead->next = ammoPool->next;
	ammoPool->next = dead;
}

void jetHeroAmmo::clear()
{
	jetAmmo *cur;
	jetAmmo *del;

	cur = ammoRoot->next;

	while ( cur )
	{
		del = cur;
		cur = cur->next;
		dieAmmoDie(del);
	}

	ammoRoot->next = 0;

	
}

void jetHeroAmmo::addAmmo( float pos[3] )
{
	float	vel[3] = { 0.0, 0.0, 0.0 };

	jetAmmo *newAmmo = 0;
	jetAmmo *first = 0;

	vel[1] = 5.0 * game->getSpeedfactor();

	fprintf( stderr, "Bullet velocity: %f \n", vel[1] );

	fprintf(stderr, "HeroAmmo::addAmmo( <%f %f %f> )\n", pos[0], pos[1], pos[2]);
	newAmmo = getNew();
	newAmmo->init(pos, vel, ammoDamage );
//	fprintf( stderr, "Player - p0: %f, p1: %f, p2: %f \n", game->hero->pos[0], game->hero->pos[1], game->hero->pos[2] );
	newAmmo->back = ammoRoot;
	newAmmo->next = ammoRoot->next;

	if ( ammoRoot->next )
	{
		first = ammoRoot->next;
		first->back = newAmmo;
	}

	ammoRoot->next = newAmmo;
}

void jetHeroAmmo::updateAll()
{
	jetAmmo *thisAmmo;

	thisAmmo = ammoRoot->next;

	while( thisAmmo )
	{
		if ( thisAmmo->pos[1] > 9.0 )
		{
			jetAmmo *backAmmo = thisAmmo->back;
			jetAmmo *nextAmmo = thisAmmo->next;

			backAmmo->next  = nextAmmo;

			if ( nextAmmo )
				nextAmmo->back = backAmmo;

			dieAmmoDie( thisAmmo );

			fprintf( stderr, "Ammo dead. \n" );

			thisAmmo = nextAmmo;
		}
		else
		{
			thisAmmo->updatePos();
			thisAmmo = thisAmmo->next;
		}
	}
}

void jetHeroAmmo::drawGL()
{

	jetAmmo *thisAmmo;;
	float *pos;

	glColor4f( 1.0, 1.0, 1.0, 1.0 );
	glBindTexture( GL_TEXTURE_2D, ammoTex );
	thisAmmo = ammoRoot->next;

	glBegin( GL_QUADS );
		while ( thisAmmo )
		{
			pos = thisAmmo->pos;
			fprintf( stderr, "p1: %f, p2: %f, p3: %f \n", pos[0], pos[1], pos[2] );
			glTexCoord2f( 0.0, 0.0 ); glVertex3f( pos[0] - ammoSize[0], pos[1], pos[2] );
			glTexCoord2f( 0.0, 1.0 ); glVertex3f( pos[0] - ammoSize[0], pos[1] - ammoSize[1], pos[2] );
			glTexCoord2f( 1.0, 1.0 ); glVertex3f( pos[0] + ammoSize[0], pos[1] - ammoSize[1], pos[2] );
			glTexCoord2f( 1.0, 0.0 ); glVertex3f( pos[0] + ammoSize[0], pos[1], pos[2] );
			thisAmmo = thisAmmo->next;
			//fprintf( stderr, "p1: %f, p2: %f, p3: %f \n", pos[0], pos[1], pos[2] );
		}
	glEnd();
}


jetAmmo.cpp
#include "jetAmmo.h"

#include <stdio.h>

int jetAmmo::ammoCount = 0;

jetAmmo::jetAmmo()
{
	back = 0;
	next = 0;
	ammoCount++;
}

jetAmmo::~jetAmmo()
{
	ammoCount--;
}

void jetAmmo::init( float pos[3], float vel[3], float damage )
{
	pos[0] = pos[0];
	pos[1] = pos[1];
	pos[2] = pos[2];

	vel[0] = vel[0];
	vel[1] = vel[1];
	vel[2] = vel[2];

	damage = damage;

	fprintf( stderr, "Ammocount: %d \n", ammoCount );
}


Chances are i have left out some peice of vital information, so feel free to yell at me or whatever. Thanks for yer input.

Share this post


Link to post
Share on other sites
jetAmmo.h
[source lang=cpp]
#ifndef JETAMMO_H
#define JETAMMO_H

#include <stdio.h>

class jetAmmo
{
public:
jetAmmo();
~jetAmmo();

void init( float pos[3], float vel[3], float damage );

inline void updatePos() { pos[0] += vel[0]; pos[1] += vel[1]; pos[2] += vel[2]; }

float pos[3];
float vel[3];
int damage;

jetAmmo *next;
jetAmmo *back;

private:

static int ammoCount;

};

#endif



jetHeroAmmo.h
[source lang=cpp]
#ifndef JETHEROAMMO_H
#define JETHEROAMMO_H


#include "jetGame.h"
#include "jetAmmo.h"
#include "jetHero.h"

#include <windows.h>
#include <gl/gl.h>
#include <gl/glpng.h>

class jetHeroAmmo
{
public:
jetHeroAmmo();
~jetHeroAmmo();

void addAmmo( float pos[3] );
void updateAll();

void drawGL();
void clear();

void loadTex();
void delTex();

private:

jetAmmo *getNew();
void dieAmmoDie( jetAmmo *dead );

jetAmmo *ammoPool;
jetAmmo *ammoRoot;
float ammoSize[2];
float ammoDamage;

GLuint ammoTex;

jetGame *game;
};

#endif



jetHero.h
[source lang=cpp]
#ifndef JETHERO_H
#define JETHERO_H

#include <windows.h>
#include <gl/gl.h>

class jetGame;
class jetHeroAmmo;

class jetHero
{
public:
jetHero();
~jetHero();

void fireGun();

void moveEvent( int x, int y );

void update();
void drawGL();

bool isShowing() { return m_show; }

void loadTex();
void delTex();

float pos[3];

private:
float size[2];
float bound[2][2];

bool m_show;

GLuint maintex;

jetGame *game;

};

#endif



Share this post


Link to post
Share on other sites
I don't see where you output the ship position. Therefore any number of things could take place between that, and the bullet position being output. There may be no explicit calculations but something else might be getting trashed.

What are the actual classes for? Why two ammo classes?

Why use arrays of 3 floats when you could use x,y,z ? Not only is the latter more obvious but it's less typing. You rarely need to iterate over your coordinates anyway.

I can spot one big bug though, which may be an issue: look at all but the last line of jetAmmo::init.

Share this post


Link to post
Share on other sites
your logical scope is wrong:

void jetAmmo::init( float pos[3], float vel[3], float damage )
{
pos[0] = pos[0]; // you're just reassigning the local variabls to themselves.
pos[1] = pos[1];
pos[2] = pos[2];

vel[0] = vel[0];
vel[1] = vel[1];
vel[2] = vel[2];

damage = damage;

fprintf( stderr, "Ammocount: %d \n", ammoCount );
}
change it to:

oid jetAmmo::init( float pos[3], float vel[3], float damage )
{
this->pos[0] = pos[0];
this->pos[1] = pos[1];
this->pos[2] = pos[2];

this->vel[0] = vel[0];
this->vel[1] = vel[1];
this->vel[2] = vel[2];

this->damage = damage;

fprintf( stderr, "Ammocount: %d \n", ammoCount );
}
thats what you get for shit naming conventions :P
use and underscore if you insist on having function arguments share the same
name as member function arguments, you should update all your other functions
as well.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this