Friday, October 23, 2015

python - How do I create a selection rectangle?


I have a basic prototype working with health, resources and units. The problem is: How do I select multiple units? I have an idea of making an rectangle which dynamically extends with the mouse position. I think I need to convert this rectangle to transparent rectangle.



Answer



pygame.draw.rect should be enough to draw transparent rectangles, according to documentation if you use width > 0, then the rectangle won't be filled.



2d - Combinations for tiling two textures together


Hi I need to create seamless ground with grass and dirt. I can make two textures connect seamlessly one to another. My question is how to figure out all the combinations, like (left, top, bottom, left-bottom, right-botton-left and so on)? Is there some kind of guide? Thank you in advance.



Answer



There's a solid overview of different popular tile combination patterns put together by Boris the Brave, building on some earlier work by Sean Howard.


I'll briefly summarize the main categories these authors identify in case the links break in future, along with a few additions of my own. I recommend you read the original articles for the full details.


Rug


4x4 array of tiles with borders at the edges.
(16 tiles)



Draws rectangular boxes of various sizes, but can't do concave shapes. Often used for UI message boxes or cliff faces in RPG tilesets.


If you use just the top-right 3x3 section, it's commonly called a "9-Slice" - you lose the ability to form shapes smaller than 2x2 tiles though.


Fence


4x4 array of tiles with connections across shared edges.
(16 tiles)


Draws ribbons and networks of tiles connected along their shared edges.


Both Rug and Fence are examples of Wang Tiles with two edge types, they just differ in whether the corners are counted as "inside" or "outside"


Marching Squares


Tiles including all combinations of 2 foreground/background textures by 4 corners. or Alternative layout that keeps similar edges together
(16 tiles, +2 optional alternate diagonal tiles)



You may also see this style called Corner Tiles - they're the equivalent of Wang Tiles with matched corners instead of matched edges. In another answer I show how this layout can be extended to three different corner textures.


Inside/Outside


3x3 box of tiles and 4 concave inner corners, plus a background tile.
(14 tiles)


I see this subset of Marching Squares without the diagonals fairly frequently, but I haven't found an officially accepted name for it, so I mocked up the example above using Boris's tiles. Alexandre Vaillancourt's example in the other answer is another layout of this pattern.


The Blob


48 permutations of foreground/background edge and corner styles.
(48 tiles)


This tileset covers an exhaustive collection of corner and edge states, where a foreground corner is always bounded by two foreground edges, but a foreground edge is allowed to slip between two background corners.


Caeles and others at OpenGameArt have investigated various ways of packing tilesets similar to this one - you can find several "blob" templates there.



Alt blob layout as an 8x6 Alt blob layout as a 7x7


Sub-Blob


20 quarter-tile pieces.
(5 tile-equivalents: 20 quarter-tile pieces that can be assembled into full tiles)


Surprisingly, you can make all 47 non-blank (all-background) tile combinations in "the Blob" with just these 20 corner pieces, assembled in different combinations.


This is used in RPG Maker VX's autotiles and other autotiling systems to cut down on the amount of unique texturing needed to flexibly cover a wide array of shapes.


Note that the quarter-tile split puts some restrictions on how much you can bevel the corners, sometimes leading to a more blocky appearance than when using full tiles, as illustrated by Felix Trapper here:


Cross-fade between similar maps with and without full-tile corners.


Extended Autotile


9x9 grid of tile/subtile parts and two extra slots.

(11 tile-equivalents: same 20 quarter-tiles as Sub-Blob, plus 4 dedicated corner tiles and some redundancy)


Here I'm borrowing the name given to this by the Rotorz tile system. It's also the style used by RPG Maker XP. The extra corner tiles can support a wider turning radius, or a diagonal split between foreground & background, which can help a map look more organic. Note that this particular layout includes some redundant sub-tiles (shown darkened above).




Here's a table summarizing what you get with each of the combinations:



9-Slice Rug Fence M.S. I/O Blob S.Blob Ex.A
Tile equivalents 9 16 16 16 14 48 5 11
...with all-blank +1 +1 +1 incl incl incl +1 +1
Smallest foreground 2 1 1 2 2 1 1 1
Concave corners N N Y Y Y Y Y Y

Foreground diagonals N N Y Y N Y Y Y
Background diagonals N N N N N N N N

Thursday, October 22, 2015

collision detection - How do I calculate distance between a point and an axis-aligned rectangle?


I have a 2D rectangle with x, y position, height and width, and a randomly positioned point nearby.


Is there a way to check if this point might collide with the rectangle if it is closer than a certain distance? Imagine an invisible radius outside of that point colliding with said rectangle. I have problems with this simply because it is not a square!





c++ - How can a resource manager have dynamic loading/unloading without creating dangling pointers?


I have a resource manager that manages the lifetime of resources in memory. A "resource" is something like a texture, mesh, et cetera. I also have an entity class that, the base class for things in my game world, which has pointers to individual resources.


I'm thinking about giving my resource manager a memory quota so it can free and reload resources as it needs to in order to satisfy this quota, and I'm wondering how I could go about this without ending up with dangling pointers in my entity objects.


I'll need the ability to free up a mesh or texture at any time in the resource manager.



I could use shared_ptr in the resource manager and weak_ptr in the entities (as long as I don't expose a shared_ptr out side of the resource manager, I'd still get the ability to free them on demand), but I am more interested in how you would do it without those classes, for example before C++11 existed or before Boost's smart pointer library existed.



Answer



Instead of holding raw pointers, hold some other lightweight reference to the resource. A shared_ptr and corresponding weak_ptr instances are one such mechanism, but you can easily implement your own if you don't have access to those types (or their earlier Boost equivalents) or there are other reasons why those types would not be appropriate for your scenario (such as being too heavyweight).


This kind of 'handle' system can be as simple as handing out integers which refer to internal slots in the resource system, which can then be used to dynamically reload a resource if access to it is requested while it's been unloaded.


In practice you probably want something a little more robust, at least a way to treat the handle "like" a pointer and provide operator -> overloads, for example, by wrapping it in a Handle type. That Handle type can just contain a pointer to a "control block" stored within the resource manager. The control block has the raw resource reference and a boolean flag that indicates if the resource is alive or not, and some kind of key that can be used to reload the resource if needed (I assume a string in the example below). A very simple example of this might look like:


template 
struct Handle {
Resource * operator -> () }
return m_owner->retrieve_resource(m_resource_id);
}


private:
int m_resource_id;
resource_manager * m_owner;
};

namespace detail {
struct control_block {
Resource * resource;
bool is_alive;

std::string key;
};
};

struct resource_manager {
Resource * retrieve_resource (int id) {
auto block = m_resources[id];
if (block->is_alive) {
return block->resource;
} else {

block->resource = reload_resource(key);
block->is_alive = true;
return block->resource;
}
}

private:
std::vector m_resources;
};


In this implementation, the resource manager just stores a flat array of control blocks which can be referenced by an integer, and provides a method to recover a resource pointer by ID, reloading that resource if needed. The Handle class is just a useful wrapper around that behavior. Obviously this is a very simple example, though. If you want something more complete, consider Scott Bilas' article on a generic handle-based resource manager. It takes a somewhat different approach, but is well worth the read.


(Be warned that a need your question implies may mean your resource utilization is very chaotic in nature, and that isn't necessarily a good thing for performance, so you might want to consider that.)


conjunctions - Yet or Although?



The new service was expected to be a success; ____very few customers upgraded their accounts.
A. yet
B. just
C. moreover
D. although




In this sentence, I am confused between “yet” and "although". I think although is more correct, but the answer is yet. Please help me make clear about the usage of yet. Thank you very much




Wednesday, October 21, 2015

meaning - It's time I go to bed vs It's time I went to bed?


What does It's time I went to bed mean?


and can we say It's time I go to bed ?


and what's the difference between the two sentences?




word usage - A while ago and a few minutes ago


I was told that another alternative for 'a few minutes ago' is 'a short while ago'. Let's say I am a student of a 20- minute English phone class and my teacher asks me questions which I answer during the first 10 minutes of our class. On the second 10 minutes she corrects my sentences. She says, "A while ago you answered, 'I go to work yesterday'. It should be 'went'. Past tense." Is it correct to use 'a while ago' and not 'a short while ago'? The situation is very clear. We never talked before the 20-minute class. It means that she refers to the period of time within the 20-minute class. Thank you.




Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...