Advertisement

collision sprite

Started by November 16, 2018 10:13 PM
33 comments, last by Rutin 6 years ago

I am made a lot of progress in my game but I have hit a small snag. in my code I am using an AABB collision detection. I have also used exit(0) to debug my code.it tells  me where in my code where it is getting access to. I have put exit(0) in the AABB collision detection and it accesses it just fine. but when I put only drawcollision_one function in the AABB collision routine it does not draw my collision sprite  animation. when I put drawcollision_one in the display function it works just fine. when the planes collide nothing happens but the exit(0) is accessed when the planes collide. here is the code I am working on.


void drawcollision_one()
{
	glEnable(GL_TEXTURE_2D);

	glBindTexture(GL_TEXTURE_2D, texture[3]);

	glBegin(GL_POLYGON);
	glTexCoord3f(0.0f + screen, 0.0f, 0.0f);

	glVertex3f(0.5f, -0.5f, 0.0f);
	glTexCoord3f(0.167f + screen, 0.0f, 0.0f);

	glVertex3f(0.5f, 0.5f, 0.0f);
	glTexCoord3f(0.167f + screen, 1.0f, 0.0f);

	glVertex3f(-0.5f, 0.5f, 0.0f);
	glTexCoord3f(0.0f + screen, 1.0f, 0.0f);

	glVertex3f(-0.5f, -0.5f, 0.0f);
	glEnd();

	glDisable(GL_TEXTURE_2D);
}

void timer(int val)
{
	screen += 0.1667f;
	if (screen >= 1.0f)
	{
		screen = 1.0f;
	}
	glutPostRedisplay();
	glutTimerFunc(500, timer, 0);
}

void coll_plane_one()
{
	//draw bullet
	float x = -5.0f + horizontal;
	float y = 0.0f + vertical;
	float oWidth = 1.0f;
	float oHeight = 1.0f;
	//draw plane
	float xTwo = 5.0f + horizontal_one;
	float yTwo = 0.0f + vertical_one;
	float oTwoWidth = 1.0f;
	float oTwoHeight = 1.0f;

	if (checkCollide(x, y, oWidth, oHeight, xTwo, yTwo, oTwoWidth, oTwoHeight) == 1)
	{
		drawcollision_one();
	}
}

 

20 minutes ago, phil67rpg said:

here is the code I am working on.

Do you have a question about your code?

-- Tom Sloper -- sloperama.com

Advertisement

how do I implement my drawcollision_one function so that when I detect a collision it draws my collision animation.

21 minutes ago, phil67rpg said:

how do I implement my drawcollision_one function so that when I detect a collision it draws my collision animation.

Generally you would set an animation trigger. Once collision has been detected in your object class you should toggle something in your animation manager that starts a timer and depending on where you are in the time line certain parts of the animation strip will draw.

Programmer and 3D Artist

actually my animation sprite works just fine I just have a problem triggering it with my collision detection routine.

26 minutes ago, phil67rpg said:

I have used the glutTimerFunc function but it does not work

I don't use GLUT, and I know I've suggest you don't use it either prior because it's not good for production applications. I also have zero idea on how timers work in it so I can only provide a generalized answer.

My answer will be based on getting the elapsed time and may not necessarily be the proper or best answer if GLUT can make its own timers. If you're able to take the current time at animation entry, and take the elapsed application time each update you can get a timer that way.

Example:

Animation starts -> Set the elapsed time to a float variable as animStartTime.

Each Update step you can see where you are in time because Elapsed Time - animStartTime = Current Time since you started the animation sequence.

So if you're at 5000 milliseconds when your animation starts, you set that to animStartTime, then every frame as you cycle through your animation logic you'll be getting the time based on the following -> Elapsed Time (7000 ms for example) - animStartTime (5000 ms) = 2000 ms, so whatever frame is supposed to be active at 2 seconds would be set.

Otherwise you can use tick rates if you're running it on a fixed time step and only allow the animation to run (x) ticks from entry.

If you want to do it the GLUT way with timers, then you'll have to wait or find someone that actually uses GLUT. Another option if possible is to use another library to handle your time for you if possible. This is the consequence in using something that is depreciated. I'm not sure why you don't move into something like GLFW: http://www.glfw.org/ as opposed to GLUT.

I've also read that GLUT isn't good for animation work due to lack of precision timing, but again I don't know.

Programmer and 3D Artist

Advertisement
1 hour ago, Rutin said:

 

1 hour ago, Rutin said:

 

I am sorry but glutTimerfunc does work for animation but my problem is with the collision detection function

 

Well you said the latter, then edited your post stating an entirely different issue... 

Regarding your collision detection, what have you tried, and when you run your debugger what is occurring? You're calling a function checkCollide() but where is the code? If your issue is with collision detection it would make sense to include the code that actually does it.

Does your collision code even work or is the issue starting the animation sequence once detection is true?

Programmer and 3D Artist

here is my collision detection function


bool checkCollide(float x, float y, float oWidth, float oHeight, float xTwo, float yTwo, float oTwoWidth, float oTwoHeight)
{
	// AABB 1
	float x1Min = x;
	float x1Max = x + oWidth;
	float y1Max = y + oHeight;
	float y1Min = y;

	// AABB 2
	float x2Min = xTwo;
	float x2Max = xTwo + oTwoWidth;
	float y2Max = yTwo + oTwoHeight;
	float y2Min = yTwo;

	// Collision tests
	if (x1Max < x2Min || x1Min > x2Max) return false;
	if (y1Max < y2Min || y1Min > y2Max) return false;

	return true;
}

 

Does it work for you???

Programmer and 3D Artist

This topic is closed to new replies.

Advertisement