Sign in to follow this  
ChesStrategy

Rendering Enemies in a 2D Platformer

Recommended Posts

ChesStrategy    122
Maybe, I'm simply confusing myself and making it seem harder than it really is, but then again...maybe many things do have to be considered. Anyways, I'm currently trying to make a simple 2D platformer. When considering how I would go about displaying enemies on the screen so that they could move, attack, and do whatever other actions they do, while the "hero" moves as well, I believed threading would be sufficient (without giving it much thought). But now as I think about it, do we really need to get into the complications of threading? If each enemy and their animations were each a thread, we would not be able to guarantee a certain order in which the animations would play out, since (based on my limited knowledge of threads), the CPU would select which thread to run arbitrarily. I guess the solution to the threaded method would be implementing a priority system of sorts. But with all these considerations, I wonder if there is a better implementation for rendering multiple enemies and their animations? Ultimately, we all know that a computer runs code linearly, as in, line by line. The speed of the computer makes our perception of any game were playing seemingly have simultaneous events occurring. So, most of the time, would a simple algorithm like this suffice (for a 2D platformer)? 1. Check to see what animated objects are currently seen by the scrolling camera. 2. Compile a dynamically allocated array of objects that need to move a "conceptual" step in their animation. 3. Iterate through the array and update each enemies'/hero's animation frame and/or position by some increment 4. Finally do a collision detection check and update/resolve positions if there is a collision

Share this post


Link to post
Share on other sites
Aardvajk    13205
Generally, you want to animate and move enemies even if they are off screen, otherwise when an enemy moves off screen, it won't come back on again. If you had a rover moving left and right, for example, if it wandered off the screen, you'd want it to move back on again.

If you found a performance hit, you could perhaps optimise to test how far from the camera centre an object was before deciding whether to update it or not, but this is unlikely to be a bottle-neck in a 2D sidescroller.

You are certainly over-engineering (no offense intended [smile]). Threading would be very much overkill for solving this.

Personally, I just call Update(float Delta) on every object in my game each game loop. Update() is a virtual method of Actor, from which enemies, the player, pickups etc all derive. I have all my Actors in a vector and just iterate through that each turn, calling Update() with the time delta.

Share this post


Link to post
Share on other sites
cclyde    184
If you were going to multithread, Id not have one thread for every enemy, rather one thread for updating all enemies.

I would personally forget about multithreading this. If your AI code is extremely slow for some reason, try and optimize it before threading it, and just update it like EasilyConfused said.

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