Move the circle along the pipe - c#

the circle is slightly larger than the pipe and I want to make it move along it so that it changes its angle depending on the rotation of the pipe. That is, in fact, so that the circle is always inside the pipe. But I have no idea how to do it
I tried to release 4 beams from the circle directed at the pipe, but again I have no idea what to do next

Related

How to make a Chunks in Unity 2D

Create 2D Top Down Chunks
I was wondering how I can make a chunk system in Unity2D. My map is too big, I want to make a system of chunks, to unload and load the parts of the map, as the player progresses.
I need to do this for the game to be more optimized, but I don't know how to do that.
I've already tried to create some code or search for examples on the internet, but unfortunately I didn't find anything.
The problem in the question can be solved with a lot of strategies, but all of them require good programming experience.
I can give you an idea to send you on a good path, but without writing code, because for each project the code would change.
Think you have 4 parts. These parts are rectangular or square and form a large rectangle much larger than the player's view.
The player can start from any position, but for the example imagine that he is in the first rectangle, the one at the bottom left.
We call the rectangles A, the lower left one, B the lower right one, C the upper left one, and D the upper right one.
When the player goes to the right, walking for a while he will arrive in rectangle B.
If it continues to move to the right at some point it will arrive at the end of rectangle B and at this point you have to place rectangle A, which is on the left, next to the right side of rectangle B, so that the player does not see the end.
We now have rectangle A on the right and rectangle B on the left.
If the player continues to go to the right, he will reach the boundary of rectangle A, and there you have to move rectangle B to the right of the terrace A, as in the initial situation.
If the player goes to the left you have to think in the opposite way.
If it goes up, you have to think about the same system, but instead of with rectangles A and B, with rectangles A and C or B and D: it depends on which one it is.
This is one way to have an infinite map.
You can customize this system to have not 4 blocks but more, and to get the result you want.

Snaping circle to rectangle/line

I am trying to implement some kind of snapping functionality in WPF for a circle (which represents my mouse) and it should snap to another object (normally this would be a line or a rectangle).
Is there a way to do this kind of functionality with WPF without doing all the calculations on my own and if not is there an easy way (library?) to get this kind of information?
Edit: I want to snap the border of the circle to the border of the rectangle/line.
As a first step, you should find the point on the rectangle that is the closest to the cursor, and the distance between the two: extending the edges of the rectangle, you partition the plane into 9 regions. Depending on the region where the cursor lies, the searched distance will be the distance to a corner (Euclidean distance formula) or the distance to an edge (difference of abscissas or ordinates).
Subtract the circle radius from this distance. This will tell you if you are close enough for a snap.
When a snap is possible, move the cursor along the line from the current cursor position to the closest point until you hit the corner or edge. You will need to use the parametric equation of the line segment.
The complete discussion requires some care but only involves simple math.
A similar approach is possible to snap to a line segment. Here is a trick: if you rotate the line segment to make it horizontal, you can consider the line segment as a degenerate rectangle and use the same snapping algorithm. Rotate the line segment and the cursor, apply the snapping logics and then counter-rotate the updated cursor.
That kind of functionality only takes a few lines of code to replicate... I doubt that you'll find a 'library' of code to do it for you. The method is as follows:
Keep a collection that contains the 4 Points that form each shape's bounding box. You then need to handle to MouseMove event on the Canvas, or shape container. In this event, you simply need to ascertain whether the current mouse position is within a certain distance from any of the shape edges... you'll have a little bit more work to do with non-rectangular shapes to calculate their edges, but the principal is the same.
If you detect the presence of a nearby shape, then you simply need to change the value of the nearest dimension to that of the nearby shape... the snap. That's it... much easier than you think.

Detect mouseover of non-square part of an Image

So I am working on a Risk type game in XNA/C#. I have a map, similar this one, and I need to be able to detect mouseovers on each territory (number). If these areas were squares, it would be easy, as they could each be represented by a rectangle. However, they are different size polygons. Is there a polygon shape that behaves similar to a square? If there isn't, how would I go about doing this?
I sugest this:attach color to each number, recreate your picture in these colors: every shape will be in its particular color. Dont draw it onscreen, use it only as reference map. And when the user clicks or moves mouse over your original map, you just simply project mouse coordinates into the color map, check the color of pixel laying under the mouse and because you have each color associated to number of territory...
This is not c# specific (as I've never written anything in the language, so no idea of what apis there are), though there are 2 algorithms that come to mind for detecting if a point is inside a polygon (which can be used to detect if a mouse point is over another polygon/map shape).
One is based on raycasting, where you cast a ray in 1 direction from the (mouse) point to "infinity" (edge of the board in this case) and count the number of times it crosses the polygon's edges. If it is odd, then the point is inside the polygon, if it is even, then the point is outside of the polygon.
A wiki link to it: http://en.wikipedia.org/wiki/Point_in_polygon#Ray_casting_algorithm
The other algorithm that comes to mind works only for triangles I think but it can be more simple to implement I think (taking a quick glance at your shapes, I think they can easily be broken down into triangles and some are already triangles). It is to do with checking if the point is on the same (internal) "side" of all the edges in the triangle. To find out what "side" a point is on vs an edge, you'd take create 2 vectors, the first vector would be the edge itself (made up of 2 points) and the other vector would be the first point of that edge to the input point, then calculate the cross product of those 2 vectors. The result will be negative or positive, which can be used to determine the "direction".
A link to it: http://www.blackpawn.com/texts/pointinpoly/default.html
(On that page is another algorithm that can also work for triangles)
Hit testing on a polygon is not so difficult to do in real time. You could use a KD-Tree for optimisation if the map is huge. Otherwise find a simple Contains method for a polygon and use that. I have one on another computer. Let me know if you'd like it.

Get points for a semi circle

The title says it.
This link(Wikipedia Midpoint Circle Algorithm) shows how to get points for a full circle, now I need it to be for a semi circle(an arc?).
It semi circle should be facing up, like in this image(Check this image)
But the bottom of the circle should be open!
For those who might think this is homework, it's not.
I'm working on a game in Xna and I want the 'rocket' that comes out of the rocket launcher to go through a certain path, a semicircle.
I don't think that algorithm is the right way to do what you want to do: describe a path. That algorithm is for plotting pixels along the path rather than describing it as such. Instead, trigonometry is probably what you want. Increase the angle from one point to another, step by step. A circle is 2π radians, so half a circle is π radians.
This should provide what you need to describe the arc. http://mathworld.wolfram.com/Trigonometry.html
Just use the Midpoint Circle Algorithm for values where y > 0 (assuming that the circle's midpoint is at (0, 0)). Anything below that is "open"

Starfield Screensaver Equations

For those of you who don't remember exactly what the old windows Starfield screensaver looked like, here's a YouTube video: http://www.youtube.com/watch?v=r5AoFiVs2ME
Right now, I can generate random particles ("stars") inside in a certain radius. What I've having trouble doing is figuring out the best way the achieve the affected seen in the afore-linked video.
Question: Given that I have the coordinates (vectors) for my randomly generated particles. What is the best way and/or equation to give them a direction (vector) so that they move across the screen in a way which closely resembles that which is seen in the old screensaver?
Thanks!
They seem to move away from the center. You could try to calculate the vector from the center point of the screen to the generated particle position? Then use the same direction to move the particle and accelerate the particle until it is outside the screen.
A basic algorithm for you to work with:
Generate stars at random location, with a 3-D gaussian distribution (middle of screen most likely, less likely as you go farther from the screen). Note that the motion vector of the star is determined by this starting point... the motion will effectively travel along the line formed by the origin point and the starting location, outward.
Assign each newly generated star a distance. Note that distance is irrespective of starting location.
Move the star in a straight line at an exponentially increasing speed while simultaneously decreasing it's distance. You'll have to tweak these parameters yourself.
The star should disappear when it passes the boundary of the screen, regardless of speed.

Categories