Game Events based on Physics Interactions

posted in JWColeman for project Gogger
Published November 16, 2018
Advertisement

So, last night I jumped a giant hurdle in design with regards to triggering game events. The problem I faced was that I had two systems that were decoupled, Game and Physics, however, the goal was to generate a game event from a physics interaction, in this case, an AABB collision. This required the use of a callback function, which I determined logic for in the Game Class, but registering it with the physics class, so that the physics could use the callback to initiate game events. In my case, I wanted to make an explosion, so here we have this bit:

 


	this->onIntersect = [this](PhysicalObject & a, PhysicalObject & b)
	{
		GameObject * objectA = reinterpret_cast<GameObject*>(a.userData);
		GameObject * objectB = reinterpret_cast<GameObject*>(b.userData);

		if (objectA->Type == GameObjectType::ENEMY && objectB->Type == GameObjectType::PLAYER)
		{
			GenerateExplosion(objectA->getPosition(), 64, 64);
			objectB->resetPosition();
		}
	};

 

The call back takes two physical objects that have collided and reinterprets their void userData pointer into a game object. Then, after determining what game object types we're dealing with, we apply the logic, in this case, we want to generate an explosion, and for now, I'm resetting the goblin's position to its origin. The GameObjectType is something I just newly added to this process, and it has also enabled me to gate the response of the trigger objects, so that the player is not reset back to its origin if it runs into a lane trigger box (the thing that causes my lanes to reset).

 

Another problem was identified upon successfully generating an explosion, it didn't animate! This initiated a long string of changes to how my game objects handle their animations. My game object was previously reliant on whether or not the object was in motion (a boolean provided by the physical component) to determine whether it should animate or not. Therefore, no movement, no animation. This caused me to restructure how I handle animations. Now, I have set it up so that doing nothing is actually running an IDLE animation, even if that animation is just one frame. Now, things are always animating. There were a lot of ins and outs to this change but I'll post the updated game object update code so that you can get an idea:


void GameObject::Update(float dt)
{
	Position = myPhysics->getNewPosition(dt);
	if (myPhysics->getDirection() >= 0)
	{
		currentDirection = myPhysics->getDirection();
	}
	mySprite->setPosition(Position);
	mySprite->SetAnimation(currentAction, currentDirection);

	if (updateTimer.GetMilisecondsElapsed() >= mySprite->getFrameTime())
	{
		mySprite->AdvanceFrame();
		updateTimer.Restart();
	}
}

 

Changes summarized:

  1. Game object needed to be aware of its current facing direction
  2. Set animation needed modified to take an action, and a direction, this was quite a large undertaking, taking around 3 hours for me to get working. I had to change the way I initialized my animations, as well as use the currentAction and currentDirection to get a key into the animations hash map.
  3. Remove dependency of bool inMotion to determine whether or not to animate
  4. Add idle animations to everything, and a default animation set as IDLE/UP if there is only one animation
  5. Cause all game objects to start in facing position UP
  6. etc... the list goes on

Here is the result!

Game Event Triggers and Updated Animations!.mp4

This has paved the way for me to meet some of the other requirements of the challenge, namely, triggering game win and game over events.

Also, I just purchased a humblebundle pack, here: HumbleBundle RPG Pack

So, hopefully, next time I post, we'll have some vastly improved visuals and new baddies to play with! But, not before I set up some of the aforementioned triggers, not to mention the obvious, making it so my generated explosions don't perpetually exist :).

Oh, and the explosion is from here: https://mbtskoudsalg.com/explore/explosion-sprite-sheet-png/#gal_post_3691_explosion-sprite-sheet-png-3.png, after reading the site terms I decided it was safe to use.

Previous Entry Game Initialization
3 likes 0 comments

Comments

Rutin

Looking good! :) Great work on triggering animations.

November 21, 2018 10:13 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement