This project has moved and is read-only. For the latest updates, please go here.

Text / string collisions

Topics: Developer Forum, User Forum
Sep 1, 2011 at 4:45 PM

I am looking for suggestions / recommendations / examples.  I have a situation where I need to create a number of text strings and/or text boxes that need to have the same behavior as other objects.  I would also like to have these text strings be attracted to objects that they are associated with, but not to other objects.  Think of it this way; you have a cartoon character or even a planet that has a cartoon text bubble with words that flow or move around with the character or object.  but the text objects need to not overlap or get in the way of other objects, text or otherwise.  Floating text if you will that is only attracted to its parent, but doesn't get in the way of other objects.

Suggestions?  Examples?

Sep 2, 2011 at 7:28 PM
Edited Sep 2, 2011 at 7:30 PM

I wrote a text bubble handler for a game that I worked on a couple years ago and though its not totally similar to what you want I can give you a couple of tips on what I created.


Firstly, the text bubbles were on a higher layer of a 2d multiplayer game.  They weren't supposed to overlap, they had to follow a character around as they moved, and if they were partially off screen they would slide in so they were fully visible.  I did this in flash and it was professional so I can't give you an code examples.

You should also be able to determine the bubbles width and height upon creation since the text isn't going to change.  This can be used to make a simple 2d bounding box.  When created, have the bubble be made above the character somewhere. I used screen coordinates for simplicity since I had to do 'on screen' checks for one of the requirements.  I had a manager class that created, destroyed, and moved my bubbles around which had an update once per frame. Basically each frame update of the manager you'll go through every bubble one at a time and do simple movements that gently slide the bubble a few pixels in the direction it needs to go rather than setting up some big planned out movement of the bubbles. You determine the direction that a bubble will need to go based on your requirements (such as following the character) and do a little nudge each frame.  They will actually work themselves out over the course of many frames.  While updating, when you discover the bubble you are looking at is colliding with another bubble, you simply move it away from that other bubble.  When it's the other bubbles turn to do collision checks, if its still colliding it will move also but in the opposite direction. If you make your pixel nudging small enough t won't be too'll have to play around with that value.

Sep 2, 2011 at 9:02 PM

Very cool.  And thank you so much.  To add a little clarity to the discussion, it is almost as if the text strings that we are moving around are more important than the objects themselves.  In other words, flip this around and think of the text as the object, and the object as a child or less important part of the simulation.  I would like to be able to have the strings automatically bounce around and position themselves and drag the other objects with them.  It sounds backwards, but the real problem I am having is that text is usually not considered a standard physics based object.  And I would like it to be.  Free flowing with its own rules and behaviours.  Just like any other object, and the only way that I have found to do this is to turn all of the text into sprites that move themselves around based on the physics of the game.  This sounds like a really slow way to do it to me.

I guess what I could do is something similar to what you have described, and have "hidden" objects the size of the text, have the physics associated with those objects, and then just display the text wherever the objects happen to be.

Does this make sense, or is there a much easier way to apply physics to text objects?

Thanks again for your ideas.

Sep 2, 2011 at 10:23 PM
Edited Sep 2, 2011 at 10:25 PM

I can't say I follow totally, it sounds like you want the entire physics engine to be working on your text?

If having farseer physics applied to your text is something you want, for an effect or gameplay reasons..then you don't want to do my above approach.  If its just for a UI, then I wouldn't involve farseer.

My implementation was more of a UI/non-physics engine approach that makes use of simple collision boxes to prevent them from overlapping.


Since it sounds like you DO want farseer physics applied to your text I can only give some suggestions. Since you can use xna sprite fonts to render text, you can also use them to determine the width of a string when rendered. You'd probably have to do some manual line breaks so that its made up of multiple lines rather than one long line across the screen.  Once you determine the overall box shape of your text you should be able to make a farseer body and fixture of that dimension.  Then you should be able to render the sprite font at the bod's location.You probably could mess around with the joints in the game to 'connect' it to the character that spoke it.  If you group the bodies with the correct collision ids, then they won't be permitted to collide with each other and should bounce like any other physics body when they run into each other.

Sep 7, 2011 at 1:20 AM

Yes.  Unfortunately, that is what I am trying to do.  And what you suggest is very helpful and an approach I think could work.  I remember seeing a demo of letters bouncing around with other objects in a similar fashion that might give us some insights.  I will have to go look for it.  Found it.

Having said this, after thinking about your suggestions, maybe a different approach might work as well?  What if I could always constrain the text to fit within a known text box size?  Then, instead of rendering the strings/text, we just used standard farseer boxes as graphical objects with physics.  Then after the fact overlay the text for each box after the fact based on box position.  Similar to what you were suggesting at first, but with a twist.  Don't display the graphic boxes at all.  Have them render behind the scenes (if that is even possible), lookup the location for each box and then display the text at that specific location where the underlying farseer boxes are located.  This would completely avoid the need to do all of the display/on screen work and should significantly improve the performance of displaying text because it doesn't have to be rendered as sprites.

This all sounds pretty crazy to me, but if you think about it from an old dumb terminal perspective, you could then just display the text on screen via one swipe/review of the hidden physics screen with everything in the "right" location and do all of the physics work centrally on a server and with only a few passes to the data.  Remotely displaying the text as needed, and at whatever speed the device/network allowed.

Is any of this even possible, or are we delving into the bizzare, stupid and unknown.

Thanks again so much for sticking with me on this thread.  It has given me a ton of new insight.