Skip to main content
GameDev.net gamedev.net
🔒 Locked

collision sprite

Started by phil67rpg Nov 16, 2018 at 10:13 PM 33 replies 7.3k views
Original Post
phil67rpg
phil67rpg

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


Tom Sloper
Tom Sloper
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
phil67rpg
phil67rpg

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

Rutin
Rutin
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
phil67rpg
phil67rpg

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

Rutin
Rutin
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
phil67rpg
phil67rpg
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


Rutin
Rutin

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
phil67rpg
phil67rpg

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


Rutin
Rutin

Does it work for you???

Programmer and 3D Artist
phil67rpg
phil67rpg

it does

Rutin
Rutin

Okay, if your collision is working, and your animation timer is working then what is the problem? Just toggle your animation sequence upon collision.

Programmer and 3D Artist
JWColeman
JWColeman

So, in my little experiment, I haven't implemented response animations yet based on aabb collisions, but, if I did.

I would have a function in my game object that would set its action to "Hurt", likely using an enum to enumerate different actions, which would coincide with animations. I would probably name it something like "onHit" or something similar.

In my collision loop, when detecting aabb collisions, I'd likely use my existing callback function to kind of interrupt the current animation playing for my object getting hurt. During that callback, I would check whatever logic was necessary to figure out if I should call the onHit function of the object being hit.

I'm on my phone rn so I cant really pseudocode it for you. But basically, Rutin gave you the answer, but how you accomplish that is really dependent upon your current setup.


phil67rpg
phil67rpg
17 hours ago, Rutin said:

Okay, if your collision is working, and your animation timer is working then what is the problem? Just toggle your animation sequence upon collision.

I am trying to toggle the animation sequence upon collision but it does not work for some reason, it looks like it should work.

Rutin
Rutin
30 minutes ago, phil67rpg said:

I am trying to toggle the animation sequence upon collision but it does not work for some reason, it looks like it should work.

Since you're using time and not ticks the general idea is to set frames based on (x) time passed.

Once your collision detection is true you would then activate your animation sequence. You then set the draw frame based on time passed, and you use that draw frame variable to determine which part of the texture to draw in your animation strip.

I would suggest you do the following:

Create an animation manager that can be used with any object with the following features.

- Ability to set how max frames and time per frame in ms

- Ability to set and check if the animation sequence is running

- Ability to check on how much time has passed since the animation started

- Ability to check which frame you're supposed to draw based on how much time has passed

- Setting the offset amount to grab part of the texture in your animation strip based on the active frame

- Ability to end the animation sequence once the final frame has been reached

This is a basic outline and should be enough to get you started. Your problem is no longer a 'collision' problem, it's now "How do I create an animation sequence?".


Also -- Based on your code, if you're putting the updated animation frame code in drawcollision_one(); how do you expect to see the result of the animation once the collision is done and it's no longer calling that function due to no collision happening anymore? If the bullet hits the plane and keeps going or is destroyed on impact it's no longer colliding, therefor you wont see the updated frames because drawcollision_one(); isn't calling.

It makes more sense to check for collision, then toggle the animation sequence independent of actual collision happening again after the check...

Programmer and 3D Artist
phil67rpg
phil67rpg

well to test my code I have the planes collide when I put an exit(0) in my collision detection algorithm it exits the screen. however when I put my drawcollision_one() function in the collision detection code it does not draw a collision sprite. thanks rutin. I am really trying to solve this problem. I am going to work on this problem more.

Rutin
Rutin
39 minutes ago, phil67rpg said:

well to test my code I have the planes collide when I put an exit(0) in my collision detection algorithm it exits the screen. however when I put my drawcollision_one() function in the collision detection code it does not draw a collision sprite. thanks rutin. I am really trying to solve this problem. I am going to work on this problem more.

Okay. The solution to your problem is simple. Do what I posted above and run the animation sequence under your logic loop. Simply have a check that will see if plane.animationActive() == true then do what you need in there and update in the draw phase. The only thing your collision should be doing is setting the animation to active for the appropriate objects when collision is true, then you run your sequence under your logic step until it ends and set your animationActive to return false for that object. Your draw step will reflect which frames are active for each object.

Apart from coding this for you, I have no other way which I can simplify the concept so hopefully you can figure it out.

It's better to do things more logical as opposed to slapping together a band-aid approach when programming.

Programmer and 3D Artist
phil67rpg
phil67rpg

ok I will work on it

Lactose
Lactose

Putting exit in code to debug is not something I would recommend.

Research breakpoints and debugging.

Hello to all my stalkers.
phil67rpg
phil67rpg

I have made some progress, I am able to draw a collision sprite when my planes collide. all I want to do is animate the sprite sheet. here is my updated code. I just need a little hint, I have almost solved my problem.


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

void drawcollision_one()
{
	glEnable(GL_TEXTURE_2D);

	glBindTexture(GL_TEXTURE_2D, texture[3]);

	screen = 0.0f;

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

my screen variable works when I set it to 0.0f, my problem is in getting the timer function to execute.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.