Jump to content
  • Advertisement
phil67rpg

OpenGL sprite movement

Recommended Posts

void move(int v)
{
	move_right++;

	if (move_right >= 9.0f)
	{
		move_right = 9.0f;
	}
	drawScene_bug_two(move_right, 10.0f);
	glutPostRedisplay();
	glutTimerFunc(1000, move, 0);
}

void drawScene() {
	glClear(GL_COLOR_BUFFER_BIT);
	bullet();
	drawScene_bug_one(-2.0f, 10.0f);
	move(0);
	glutTimerFunc(1000, move, 0);
	coll_bug_one();
	erase_sprite();
	drawScene_ship_one();
	glutSwapBuffers();
}

well I am able to move a sprite from the center of the screen to the right. it however moves too quickly, I want it to move one sprite width to another, I also want to move slowly. I want it to move like in space invaders.

Share this post


Link to post
Share on other sites
Advertisement

Movement will depend on a few things, do you have a fully running game loop with a time step?

If you have a variable time step you'll need to move the object by using the speed and delta time. If you're using a fixed time step, then you just need to set a speed and take into account it will be moving at that rate * your tick rate per second as it's fixed.

Variable: player.x += speed * deltaTime

Fixed: player.x += speed

Share this post


Link to post
Share on other sites
Posted (edited)

I am using fixed time step and using move_right++ to move the sprite but it moves too fast. also I want it to move one sprite width at a time. maybe I should use a variable time step.

Edited by phil67rpg

Share this post


Link to post
Share on other sites
4 minutes ago, phil67rpg said:

I am using fixed time step and using move_right++ to move the sprite but it moves too fast.

What is your tick rate at because moving an image with ++ per tick would mean you're only moving 1 pixel per tick, and if you ran your loop at 20 ticks per second as an example, that means the sprite is only moving 20 pixels per second. Running ++ on movement will be faster at 100 ticks than 20 ticks for example.

You could try doing this instead:

move_right += 0.1f;

See if this slows it down.

Share this post


Link to post
Share on other sites
18 minutes ago, phil67rpg said:

also I want it to move one sprite width at a time. maybe I should use a variable time step.

Then you would move the sprite: player.x += player.width; but only allow the move to happen every (x) ticks or use a timer to move it (x) milliseconds.

Are you wanting smooth movement, or the sprite to jump around because if you move a sprite by it's width value it would be like this moving:

[x][x][x][x]

--->>>>>>

as opposed to something more smoother that transits to a point. I'm not sure if you're looking to have more of a tile based movement system which is like the old DOS games where you would move one grid spot per key press, or by moving gradually across the screen.

Share this post


Link to post
Share on other sites
2 minutes ago, phil67rpg said:

how do I determine my tick rate?

Since you said you were using a fixed time step then your tick rate will be whatever you put as your cycle rate. I really don't know without seeing your code because you might not even have a proper time step to begin with if you're asking this question. It's one of the first variables you set in your time step code.

You'll need to post your time step and game loop code for more help.

Share this post


Link to post
Share on other sites

well I have 8 pages of code and I think I have already posted the drawing portion of my game.

Share this post


Link to post
Share on other sites
Just now, phil67rpg said:

well I have 8 pages of code and I think I have already posted the drawing portion of my game.

Are you using a timer to count time passed per tick and regulate that only updates happen once every (x) milliseconds based on your tick rate? Example 30 ticks would be 1000/30 = 33.333333333333333333333333333333 milliseconds. Your tick call would be called at every interval and would wait until the next tick is due before calling again. If you don't have something like this in your code it's very likely you don't even have a proper fixed time step in place.

Again, without code nobody can help you on this.

Share this post


Link to post
Share on other sites
void collision_bug_one(float x, float y)
{
	glEnable(GL_TEXTURE_2D);//collision sprite
	glBindTexture(GL_TEXTURE_2D, _textureId_four);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

	glColor3f(1.0f, 1.0f, 1.0f);
	glBegin(GL_POLYGON);
	glTexCoord2f(0.0f, 0.0f);
	glVertex3f(x, y, 0.0f);
	glTexCoord2f(1.0f, 0.0f);
	glVertex3f(x, y-1.0f, 0.0f);
	glTexCoord2f(1.0f, 1.0f);
	glVertex3f(x+1.0f, y-1.0f, 0.0f);
	glTexCoord2f(0.0f, 1.0f);
	glVertex3f(x+1.0f, y, 0.0f);
	glEnd();

	glDisable(GL_TEXTURE_2D);
}

void collision(int value)
{
	flag[0][0] = 1;
	glutTimerFunc(1000, collision, 5);
}

void coll_bug_one()
{
	float x = -0.5f + scroll;
	float y = -8.0f + up;
	float oWidth = 0.5f;
	float oHeight = 0.5f;

	float xTwo = -2.0f;
	float yTwo = 9.0f;
	float oTwoWidth = 0.5f;
	float oTwoHeight = 0.5f;

	if (checkCollide(x, y, oWidth, oHeight, xTwo, yTwo, oTwoWidth, oTwoHeight) == 1)
	{
		collision_bug_one(-2.0f, 10.0f);
		Sleep(50);
		glutTimerFunc(25, collision, 5);
	}
}

void erase_sprite()
{
	if (flag[0][0] == 1)
	{
		glColor3f(0.0f, 0.0f, 0.0f);
		glBegin(GL_POLYGON);
		glVertex3f(-2.0f, 10.0f, 0.0f);
		glVertex3f(-2.0f, 9.0f, 0.0f);
		glVertex3f(-1.0f, 9.0f, 0.0f);
		glVertex3f(-1.0f, 10.0f, 0.0f);
		glEnd();
	}
}

void delay()
{
	float velocity = 20.0f, init_position = 0.0f;
	static int prvMs = glutGet(GLUT_ELAPSED_TIME);
	drawScene_bug_two(position, 10.0f);
	const int curMs = glutGet(GLUT_ELAPSED_TIME);
	const double dt = (curMs - prvMs) / 1000.0;
	prvMs = curMs;
	position = velocity*dt + init_position;
	cout << position << endl;
}

void move(int v)
{
	move_right++;

	if (move_right >= 9.0f)
	{
		move_right = 9.0f;
	}
	drawScene_bug_two(move_right, 10.0f);
	glutPostRedisplay();
	glutTimerFunc(1000, move, 0);
}

void drawScene() {
	glClear(GL_COLOR_BUFFER_BIT);
	bullet();
	drawScene_bug_one(-2.0f, 10.0f);
	move(0);
	glutTimerFunc(1000, move, 0);
	coll_bug_one();
	erase_sprite();
	drawScene_ship_one();
	glutSwapBuffers();
}

void shoot()
{
	up += 0.5f;
	if (up >= 17.5f)
	{
		animate = 0;
		up = move_up;
		glutIdleFunc(NULL);
	}
	glutPostRedisplay();
}

void handleKeypress(unsigned char key, int x, int y) {

	switch (key) {
	case 27: //Escape key
		exit(0);
		break;
	case 32:
		animate = !animate;
		if (animate)
		{
			glutIdleFunc(shoot);
		}
		else
		{
			glutIdleFunc(NULL);
		}
		break;
	}
	glutPostRedisplay();
}

void handleSpecialKeypress(int key, int x, int y)
{
	switch (key)
	{
	case GLUT_KEY_UP:
		move_up += 0.1f;
		break;
	case GLUT_KEY_DOWN:
		move_up -= 0.1f;
		if (move_up <= 0.0f)
		{
			move_up = 0.0f;
		}
		break;
	case GLUT_KEY_LEFT:
		scroll -= 0.1f;
		if (scroll <= -9.0f)
		{
			scroll = -9.0f;
		}
		break;
	case GLUT_KEY_RIGHT:
		scroll += 0.1f;
		if (scroll >= 9.0f)
		{
			scroll = 9.0f;
		}
		break;
	}
	glutPostRedisplay();
}

void update(int value)
{
	down -= 0.1f;
	if (down <= -20.0f)
	{
		down = 0.0f;
	}
	glutPostRedisplay();
	glutTimerFunc(25, update, 0);
}

int main(int argc, char** argv) {
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowPosition(500, 200);
	glutInitWindowSize(800, 600);
	glutCreateWindow("Bug Wars");
	initRendering();
	initRendering_two();
	initRendering_three();
	initRendering_four();
	initRendering_five();
	glutDisplayFunc(drawScene);
	glutTimerFunc(25, update, 0);
//	glutTimerFunc(1000, move, 5);
	glutKeyboardFunc(handleKeypress);
	glutSpecialFunc(handleSpecialKeypress);
	glutReshapeFunc(handleResize);
	glutMainLoop();
	return 0;
}

well here is more of my code

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!