Setting up collision groups in farseer - farseer

I'm trying to set up collision groups in Farseer so that the items in the picture collide as follows:
G1 Collides with All.
B1 and B2 collide with each other and G1, but not R1 or R2
R1 and R2 collide with each other and G1, but not B1 or B2.
I've been playing around with _Body.CollidesWith = Category.Cat1; and _Body.CollisionCategories = ..., but I'm basically just guessing. Haven't really found any usefull examples in the docs, but I might not have been looking in the right place either.
Edit 1:
Ok, so experimenting some more.
Assuming _Body is B1 (and also applied to B2) in the picture, and Cat1 is G1, and Cat2 is all blue items..
_Body.CollidesWith = Category.Cat1 & Category.Cat2;
_Body.CollisionCategories = Category.Cat2;
Should this not then allow B1 to collide with the ground (G1) and all other blues (B# items)?
Applying the above code makes all blue items collide with nothing not even each other...

_Body.CollisionCategories = Category.Cat1 | Category.Cat2;
instead of
_Body.CollisionCategories = Category.Cat1 & Category.Cat2;

Related

I am interested in drawing the solution for various ODEs and PDEs

How to draw the solution y=sinx+c for the ODE dy/dx = cosx?
I tried in excel but did not reach a solution.
I am new to this.
Please help
If you really want to do that in Microsoft Excel, then you can implement, for example, the Euler method for simplicity as follows:
B1 cell : initial value of y, for example, 0
B3 cell : initial value of x, for example, 0
B4 cell : step size, for example, 0.1, which should be sufficiently small
D1 cell : =$B$2
E1 cell : =$B$1
D2 cell : =D1+$B$3
E2 cell : =E1+COS(D1)*$B$3
Next, select the D2 cell and copy it to D3, D4, ... by dragging the fill handle, up to the point that you want to get. Then, select the E2 cell and copy it to E3, E4, ... by double-clicking the fill handle. This gives (x, y) data in D and E columns, and you can make a plot with them. The above initial values correspond to c=0 in your equation.

GraphViz dot Circular Node Alignment

How to use Graphviz to align nodes circular in clusters with additional text? Optionally with identical node positions (always 8 nodes per cluster)?
I tried circo, however, faced some shortcomings:
No clustering
No comments
Problems with margins for larger labels (10+ char)
Alignment varies with label size
This (Graphviz Online), nothing spectacular, was the closest I could get. Any hints to other layouts (or even tools) appreciated.
graph {
layout = circo;
node [shape = circle,
fontname = Helvetica,
margin = 0]
edge [style=invis]
subgraph 1 {
a1 -- b1 -- c1 -- d1 -- e1 -- f1 -- g1 -- h1 -- a1
}
subgraph 2 {
a -- b -- c -- d -- e -- f -- g -- h -- a
}
}
Not exactly the answer as I was asking for (Graphviz), but I found a much nicer solution with MATLAB. It was about plotting a seating plan for an event.
What I did broken down:
imread() image of the floor plan
Roughly determined pixel spacing, used as x & y vector for image() so that tables are in scale with the room.
Manually defined centers for the clusters (here tables) with the help of ginput() (or imellipse())
Plotted circles with plot() and added text with text()

Shape algorithm with optimization

I have a shape defined by straight line segments.
I want to simplify the shape to be constructed with straight lines but only with a finite set of slopes.
I want to minimize the amount of segments used and minimize the difference in area from the shape before and after.
I want to minimize these two things simultaneously with a user defined weight emphasizing minimizing one more than another.
minimize { J = w1(number of segments/length) + w2(difference area/length) }
Where w1 and w2 are both weights and length is the length of the new segment. I want an algorithm that does this. Any ideas?
Below I show a few pictures of how I might want it to work. Is there anything out in the literature that might help in writing an algorithm. Thanks!
This seems like a very tough problem! I would approach it by first defining two routines:
diffArea(fig, target) computes the difference area between fig and target
decomp(fig, p1, p2, s1, s2) computes the two figures that can be built by replacing all the segments between p1 and p2 with a pair of segments of shapes s1 and s2. For instance, if there were four segments between points p1 and p2 in fig, then decomp(fig, p1, p2, s1, s2) returns the two figures that are generated by replacing those four segments with appropriately scaled versions of s1 and s2. There's only one way to scale s1 and s2 to fill the space between p1 and p2 (because we're in 2-d space), and the two figures come from either ordering them s1 -> s2 or s2 -> s1.
Given these two routines, I think an iterated local search procedure might work well. You would have the following steps:
Set fig to a large bounding shape around target
For every pair of vertices (p1, p2) in fig (starting with pairs with 1 segment between, then 2 segment between, ...) and for every pair (s1, s2) of shapes:
Compute fig1 and fig2 using decomp(fig, p1, p2, s1, s2)
Let e_fig be the number of edges in fig and e_new by the number of edges in fig1 and fig2
If w1 * e_new + w2 * diffArea(fig1, target) < w1 * e_fig + w2 * diffArea(fig, target), replace fig with fig1
If w1 * e_new + w2 * diffArea(fig2, target) < w1 * e_fig + w2 * diffArea(fig, target), replace fig with fig2
Repeat this procedure until you've tested every pair of vertices and have found no improving replacements. This is obviously not going to give you an optimal solution, but I'd bet it will perform pretty well.
Well, in this case Pareto efficiency seems to be a good weight of a solution.
At first glance it seems that using a discrete optimization would be apropriate.
Choice of a particular algorithm depends on complexity of the figures to be shaped.
For large and complex figures I would suggest using genetic algorithm.

Shift trace of polygon by given vector

I need algorithm to get shift trace of given polygon by given 2d-vector.
Given valid polygon with no holes, but possibly concave.
Operation performs on plane, so the result might be a polygon, possibly with holes.
If it simplifies the task, outer polygon is enough.
It looks simple to describe, but I find it complex to realize, so I look for some ready solutions, preferably in c#.
Suppose you have a polygon P given by the points A1, A2, ... , An.
Now you decide to shift it by X on the x-axis and Y on the y-axis.
You can do this to each point individually to get the ending location of the polygon.
Let's call the shifted polygon Q givng by points B1, B2, ... , Bn.
Then all you need to do is draw the following parallelograms:
(A1 A2 B2 B1), (A2 A3 B3 B2), (A3 A4 B4 B3), ... , (An-1 An Bn Bn-1) , (An A1 B1 Bn)
At this point you will have filled in the shape you want.
Some of the parallelograms will overlap, but that is okay, since you are just filling them all in with the same red color.
By doing it this way, you also get your second example to turn out correctly, (the bottom right corner of the hole in the middle should be diagonal, because of the lip sliding into position).

Getting a list of locations within a triangle in the form of x,y positions

Let's say I have a triangle given by the three integer vertices (x1,y1), (x2,y2) and (x3,y3). What sort of algorithm can I use to return a comprehensive list of ALL (x,y) integer pairs that lie inside the triangle.
The proper name for this problem is triangle rasterization.
It's a well researched problem and there's variety of methods to do it. The two popular methods are:
Scan line by scan line.
For each scan-line you require some basic geometry to recalculate
the start and the end of the line. See Bresenham's Line drawing algorithm.
Test every pixel in the bounding box to see if it is in the
triangle.
This is usually done by using barycentric co-ordinates.
Most people assume method 1) is more efficient as you don't waste time testing pixels that can are outside the triangle, approximately half of all the pixels in the bounding box. However, 2) has a major advantage - it can be run in parallel far more easily and so for hardware is usually the much faster option. 2) is also simpler to code.
The original paper for describing exactly how to use method 2) is written by Juan Pineda in 1988 and is called "A Parallel Algorithm for Polygon Rasterization".
For triangles, it's conceptually very simple (if you learn barycentric co-ordindates). If you convert each pixel into triangle barycentric coordinates, alpha, beta and gamma - then the simple test is that alpha, beta and gamma must be between 0 and 1.
The following algorithm should be appropriate:
Sort the triangle vertices by x coordinate in increasing order. Now we have two segments (1-2 and 2-3) on the one side (top or bottom), and one segment from the other one (1-3).
Compute coefficients of equations of lines (which contain the segments):
A * x + B * y + C = 0
A = y2 - y1
B = x1 - x2
C = x2 * y1 - x1 * y2
There (x1, y1) and (x2, y2) are two points of the line.
For each of ranges [x1, x2), (x2, x3], and x2 (special case) iterate over integer points in ranges and do the following for every x:
Find top bound as y_top = (- A1 * x - C1) div B1.
Find bottom bound as y_bottom = (- A2 * y - C2 - 1) div B2 + 1.
Add all points between (x, y_bottom) and (x, y_top) to the result.
This algorithm is for not strictly internal vertices. For strictly internal vertices items 3.1 and 3.2 slightly differ.
I suppose you have a list of pairs you want to test (if this is not what your problem is about, please specify your question clearly). You should store the pairs into quad-tree or kd-tree structure first, in order to have a set of candidates which is small enough. If you have few points, this is probably not worth the hassle (but it won't scale well if you don't do it).
You can also narrow down candidates further by testing against a bounding box for your triangle.
Then, for each candidate pair (x, y), solve in a, b, c the system
a + b + c = 1
a x1 + b x2 + c x3 = x
a y2 + b y2 + c y3 = y
(I let you work this out), and the point is inside the triangle if a b and c are all positive.
I like ray casting, nicely described in this Wikipedia article. Used it in my project for the same purpose. That method scales on other polygons too, including concave. Not sure about the performance, but it is easily coded, so you could try it yourself (I had no performance issues in my project)

Resources