Jump to content
  • Advertisement
Sign in to follow this  
Runicode

Track locations of all objects and render

This topic is 3352 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm relatively new to game programming, but have extensive experience with C++. I'm currently working on a 2d game engine using the SDL API and need some advice on how to design some things. Basically I have sort of a rendering manager class whose purpose is to gather up the locations of all objects and render them. My problem is trying to decide how to track the locations of objects on the screen and determine which animation to render, whether the object is in the player's view, etc... What would be the best way of implementing this? How can I know what objects should be currently rendered? In addition to this, I would like to be able to determine when the mouse pointer is hovering over an object and based on the object, the mouse cursor, options, and on screen text may change. What would be the best method of doing this? I guess every movement of the mouse I need to check if something is in that location and take the appropriate action, but how do I know something is there? Thanks for your help! Runicode

Share this post


Link to post
Share on other sites
Advertisement
I'll try to answer your second question first. There are many ways to go about both your questions, but here's a simple way. First, let's think of the problem. You want something to happen when the players mouse hovers over something. That's basically it. Now let's think about what we already know that could help us find the solution to the problem. We know that the something that the player will hover over will be a sprite or image of some sort (as they can obviously see it). Let's assume, since you are building this is in C++, that all your "somethings" will be within a class. Let's also assume that you have a container of some sort to store all of these "somethings." SDL, im pretty sure, has a function or method that we can use to find the current coordinates of the mouse. SDL, im pretty sure, also has a function or method that can determine time for us (as like a stopwatch). So now that we have determined the problem and looked at what we knew we had to help us determine the solution its time to make our solution. Our solution is going to be inefficient (in my opinion) but simple for you to understand and work off of. Within our game loop, we need to use SDL's function for getting mouse coords, send those coords to our "something" objects, by iterating through our "something" object container. Our objects will know their current coords and based off of that will determine if the mouse coords are within their area.

obvious pseudo-code:

GameLoop
{
Coords = Get_SDL_Mouse_Coords() //< different name ofcourse

ObjectContainer iterator

for (ObjectIterator++)

*ObjectIterator.CheckMouseOver(Coords)
}

Object::CheckMouseOver(Coords Mouse)
{
if ( CheckCollision(Mouse) )
Display "Something"
}

Object::CheckCollision(Coords Position)
{
//insert collision detection algorithm here
// return true if collision detected
}




Hope this helps some. This solution will work, but it is very inefficient since you are basically iterating through an entire container and on top of that checking collisions every frame. This is extremely computationally heavy but you could very well split the objects up, or setup a timer to only do this every .5 seconds (or some other number), or set it up to do it only every 20 frames (or some other number). Maybe people here will have a better way, who knows :) The point to my post, besides helping you with your question, was to hopefully teach you to look at something from an engineering approach, whats the problem, what do I know, and how do I derive a solution from that. Good luck =)

Share this post


Link to post
Share on other sites
Hi phear - Thanks for your response! To give you some background, I have 10 years of software engineering under my belt so developing a solution of some sort really isn't the problem... it just may not be the best approach :). I should have been clearer in my post and provided my current design which is exactly the same as what you propose. So if I stick with this design, which could be a performance hit, every time an object is created or deleted, I would need to update my container that has a list of these objects. Every time the mouse is moved I'm iterating through the container and comparing the coords to the objects coords. I then retrieve the object's data members and display them on screen.

My real problem here is lets say at any given time there are 40 objects on screen in addition to the user interface which also has mouseover text. There could then be at any point 100 objects in my container and I would be constantly iterating through the container and making my comparison. I'm pretty sure without even testing this, it's going to be a noticeable performance hit.

Are there any other standard solutions to this?

Share this post


Link to post
Share on other sites


#1 Get used to thinking in terms of coordinate systems -- the screen area, your map,the objects on the map, the cursors position on the screen....

#2 When doing filtering you should do something called 'lazy evaluation' where you cull out the objects NOT within an area (the screen view window) by the cheapest method available (an XY box test for example) and then subject the remainder to a more detailed (proceesing costly) test if needed.

#3 With 100 objects being tested (even in containers with their overhead) you should be able to do such comparisons hundreds or thousands of times a second and the other processing (like the rendering or collision testing) will take much more processing time.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!