I'm currently working on the blobs (villagers). I'd like them to be self-sufficient: they should be able to feed and shelter themselves by building farms and houses automatically to meet their needs. I've started by implementing simple blobs (currently represented by inverted pyramids, which are easy/fast to render) that can move around. Currently, blob positions are (x,y) instead of (m,n) -- world space, not grid space. We'll see how well this works.
To place a blob in 3-space, I need to compute z. To do this, I need to find out which triangle the blob is in; the vertices of the triangle are centers of hexagons, for which z values are known. I then convert the blob's location to barycentric coordinates within the triangle. The barycentric coordinates (b1,b2,b3) where b1+b2+b3=1 give me weights, and the blob's z value is b1*z1 + b2*z2 + b3*z3.
The actual implementation of the z-finding function reinterprets the hexagon grid as a rhombus grid. The nice things about rhombuses is that they are just a shear transformation away from a rectangular grid. That makes it very easy to figure out which triangle you're in.
Another thing I needed was to compute the gradient at any point. The gradient tells you which direction is the steepest. If you can picture a triangle in your head, take the normal to the triangle and project it down onto the z=0 plane. That tells you what direction is the steepest downhill. I use this to implement gravity: the force applied to the blobs is proportional to the gradient vector.
You can generalize the gradient to work on any function of (x,y). If you take the gradient of moisture(x,y), then you have the direction in which the moisture decreases most rapidly. Blobs that carry water to dry areas could use that gradient as a guide. If you take the gradient of food_price(x,y), then you have the direction in which the food prices decrease most rapidly. Blobs trying to buy food should move in that direction. Blobs trying to sell food should move in the opposite direction.
Post a Comment