Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In Processing 2, How would I make this happen?
First left click of the mouse should display ball centered where mouse was when clicked.
Second left click of the mouse should display another ball centered where the mouse was when clicked.
Once both balls are displayed, a left click will launch 1st ball at the second ball.
When the 1st ball touches the edge of the second ball, the 1st ball should stop and the second ball should move in the same direction at the same speed and move the same amount of distance that the 1st ball moved
I won't give you code for this, as that would be too involved (and isn't the purpose of Stack Overflow anyway). However, I'll outline some of the principles you'll need, and hopefully you can go from there.
The first thing to do is keep track of the state. It sounds like your states will be:
Waiting for first click.
Waiting for second click.
Moving 1st ball.
Moving 2nd ball.
This approach is quite common, and is sometimes referred to as a Finite State Machine. Typically you'd define a constant integer for each state, and store a "current state" integer somewhere. It will be updated when you want to change/advance state.
In the main drawing loop, you'd execute different code depending on which state you're in. For example, in the third state, it will draw both balls, and keep on moving the 1st ball closer to the second 2nd, calculating the distance between them. When they touch, it moves on to the fourth state.
You'll obviously need a mouse handler to detect and handle clicks. That will store the ball positions and advance the state appropriately.
For the mathematical side of it, you'll need two things. First, you'll need to get comfortable using vector maths (specifically, normalising a vector to calculate direction, and multiplying it up to get a desired speed). Secondly, you'll need to use the Euclidean distance formula (basically just Pythagoras' theorem) to calculate the distance between the balls, determining when they're close enough to touch each other. There are loads of tutorials online for all this stuff which you may find useful.
If you get stuck on a particular issue in your coding, feel free to post a more specific question (although always bear in mind it may have been asked/answered already).
Related
I am making a version of the Atari game "Centipede" for my computer science class. I need help creating the collision for my code. I need to make it so when the bullets hit a part of the centipede, the game detects it and the part that got hit goes away.
This is a pretty broad question, but I'll try to help in a general sense.
You need to read up on collision detection.
First you probably want to break your centipede down into individual rectangles. For each rectangle, check whether it's colliding with the bullet.
You might consider point-rectangle collision detection, where you check whether the point is inside the rectangle. This will work if your bullet is a small point.
Or you might consider rectangle-rectangle collision detection, where you check whether two rectangles are overlapping. Use this if your bullets are larger than a point. Even if your bullet is a circle, you can usually get away with this kind of collision detection.
Please try something, and if you get stuck, then please post a MCVE that demonstrates where you're stuck. Good luck.
A simple implementation would be as follows:
1. Designate an array that represents the pixel positions of the centipede
2. Designate another array that represents the pixel positions of the bullet
3. Update each array's values based on the sampling rate of your game and check to see if there is any overlap. (Ideally have this as a separate thread)
4.Any overlap is indicative of a collision, so have some sort of collision handler function, which deletes the part of the centipede hit, that is triggered anytime an overlap event occurs.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm working on a project which involves detecting the red blood cells in the blood. RBCs in the blood are never perfectly circular (usually almost eliptical) and they often overlap.
I've searched and found a number of algorithms, but most work for circles only. However, in my case it needs to work for blood from patients with sickle cell disease, where the RBCs are elongated or sickle-shaped. For reference here is an example source image.
Can you suggest an algorithm or approach to solve this problem?
Any help would be greatly appreciated.
As mentioned in the comments, this question is really too broad to answer completely. However, I can give you some pointers in how to address this.
For starters, get yourself the MATLAB Image Processing toolbox.
"Identify red blood cells" is a deceptively simple-sounding task. The first step with any project like this is to figure out what exactly you want to achieve, then start breaking it down into steps of how you will achieve that. Finally, there is the experimental-developmental stage where you try and implement your plan (realise what is wrong with it, then try again).
Cell counting normally uses circularity to identify cells, but that's not possible here because you state you want to identify sickle cells. The other main characteristics distinguishing RBCs from other cells is the colour and size. The colour is more absolute, so start with that. Then think about size. This is a good tutorial on the process of identifying cells although it is in Python the principle is the same.
So we have:
Apply a filter to your image, either isolating the red channel (RGB) or something more complex. Make it monochrome (we don't need colour data).
Smooth the image (e.g. gaussian filter) to reduce the noise and artefacts
Find regional maxima which are (hopefully!) in the center of cells
Label the regional maxima (this should give you the number of cells)
Watershed to find the whole cells an measure size
Hopefully that is enough to get you started!
I am making a game and i have come across a hard part to implement into code. My game is a tile-bases platformer with lots of enemies chasing you. basically, in theory, I want my enemies to be able to, every frame/second/2 seconds, find the realistic, and shortest path to my player. I originally thought of A-star as a solution, but it leads the enemies to paths that defy gravity, which is not good. Also, multiple enemies will be using it every second to get the latest path, and then walk the first few tiles of it. So they will be discarding the rest of the path every second, and just following the first few tiles of it. I know this seems like a lot, to calculate a new path every second, all at the same time, if their is more than one enemy, but I don't know any other way to achieve what i want.
This is a picture of what I want:
Explanation: The green figure is the player, the red one is an enemy. the grey tiles are regular, open, nothing there tiles, the brown tiles being ones that you can stand on. And finally the highlighted yellow tiles represents the path that i want my enemy to be able to find, in order to realistically get to the player.
SO, the question is: What realistic path-finding algorithm can i use to acquire this? While keeping it fast?
EDIT*
I updated the picture to represent the most complicated map that their could be. this map represents what the player of my game actually sees, they just use WASD and can move around and they see themselves move through this 2d plat-former view. Their will be different types of enemies, all with different speeds and jump heights. but all will have enough jump height and speed to make the jumps in this map, and maneuver through it. The maps are generated by simply reading an XML file that has the level data in it. the data is then parsed and different types of tiles are placed in the tile holding sprite, acording to what the XML says. EX( XML node: (type="reg" graphic="grass2" x="5" y="7") and so the x and y are multiplied by the constant gridSize (like 30 or something) and they are placed down accordingly. The enemies get their frame-by-frame instruction from an AI class attached to them. This class is responsible for producing this path and return the first direction to the enemy, this should only happen every second or so, so that the enemies don't follow a old, wrong path. Please let me know if you understand my concept, and you have some thought/ideas or maybe even the answer that i'm looking for.
ALSO: the physics in this game is separate from the pathfinding, they work just fine, using a AABB vs AABB concept (the player and enemies also being AABBs).
The trick with using A* here is how you link tiles together to form available paths. Take for example the first gap the red player would need to cross. The 'link' to the next platform (aka brown tile to the left) is actually a jump action, not a move action. Additionally, it's up to you to determine how the nodes connect together; I'd add a heavy penalty when moving from a gray tile over a brown tile to a gray tile with nothing underneath just for starters (without discouraging jumps that open a shortcut).
There are two routes I see personally: running a quick prediction of how far the player can jump and where they'd jump and adjusting how the algorithm determines node adjacency or accept the path and determine when parts of the path "hang" in the air (no brown tile immediately below) and animate the enemy 'jumping' to the next part of the path. The trick is handling things when the enemy may pass through brown tiles in the even the path isn't a parabola.
I am not versed in either solution; just something I've thought about.
You need to give us the most complicated case of map, player and enemy behaviour (including jumping up and across speed) that you are going to either automatically create or manually create so we can give relevant advice. The given map is so simple, put the map in an 2-dimensional array and then the initial player location as an element of that map and then first test whether lower number column on the same row is occupied by brown if not put player there and repeat until false then same row higher column and so on to move enemy.
Update: from my reading of the stage generation- its sometime you create- not semi-random.
My suggestion is the enemy creates clones of itself with its same AI but invisible and each clone starts going in different direction jump up/left/right/jump diagonal right/left and every time it succeeds it creates a new clone- basically a genetic algorithm. From the map it seems an enemy never need to evaluate one path over another just one way fails to get closer to the player's initial position and other doesn't.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i want to find position of a point within grid after changing the grid shape
For Example:
In the below image i have a grid with dimensions (10,10),(20,10),(10,20),(20,20).
There is point (x) within grid and that point position is (17,13)
Now i change the dimension of the grid so grid shape gets changed
Previous grid shape get changed like this
After changing the shape of the grid
Grid dimensions are (8,8),(18,12),(12,18),(22,19)
Now what will be the position of the point (x)?
Can anyone explain the way to find the solution or
algorithm to find the current position of point..
Thanks in advance..
A basic idea:
Draw a line from one corner through the point. Record the point in the side it goes through.
Do the same for a neighbouring corner.
For the transformed square, draw lines between the same corners and where their lines went through the sides.
Where the lines cross should be where the point belongs.
A few notes:
As per its definition, a line extends infinitely.
You need to use neighbouring corners (as mentioned). If you use opposing corners, and the point is on the line between the corners, you won't be able to narrow it down beyond that line. Actually if the point can be on one of the sides, using neighbouring corners will give the same problem. In this case you'll need 3 corners.
This works because 2 lines can only + must have 1 crossing point, unless they are parallel (which can only happen in the above scenario - 2 lines containing the same point must be equal or non-parallel). If we add another corner, due to the square-shape, it can't be parallel to the other 2 lines.
Another special case pops up if 3 corners can end up on the same line. In this case, you'll need to use all 4 corners. But if all 4 corners can end up on the same line, this won't work, but in this case the resulting shape will just be a line segment.
You can also use distances, just remember to use ratio's instead of actual distances due to shape distortion.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
What I'm specifically grappling with is not just the layout of a graph, but when a user selects a graph node and starts to drag it around the screen area, the line has to constantly be redrawn to reflect what it would look like if the user were to release the node. I suppose this is part of the layout algorithm?
Also some applications get a bit fancy and don't simply draw the line in a nice curvy way, but also bend the line around the square shaped node in almost right angles. See attached image and keep in mind that as a node is dragged, the line is drawn as marching ants, and re-arranged nicely, while retaining its curved style.
alt text http://img260.imageshack.us/img260/5458/nodesr.png
If your diagrams are not crazy full you should not need an extra-fancy algorithm for this, but just use some common sense.
Cover the surface with a rectangular grid and then find a way to connect to boxes with straight lines along grid lines with a minimum number of angles: if the boxes are not on the same grid lines and you don't care where you connect you need one angle if there is no other node in between. If there are e.g. nodes in the way you need at least one more angle.
As a second step for fuller diagrams add code that not only optimizes for minimal number of edges but also for minimal length of the lines. If your diagrams are not crazy full this should be hardly noticeable in terms of application response.
For extra eye candy round the angles taking into account the lengths of both legs and checking for intersections with other objects on the surface. I would use 90°-pies of circles and adjust the radius of the circles (apparently not what was done above) -- for longer legs the radius should be bigger. Maybe the toolkit you are using can help you here.
Are you familiar with Graphviz? I'm not sure how "dynamic" and resuable the layout algorithms are, but it could be a good starting point.
Why don't you look in Dia source code to see how they are doing it?
http://live.gnome.org/Dia/Download
to extend #honk answer: for smoother curves you can just take 3 or 4 pivot points and connect them using quadratic/cubic bezier lines.
this is an example in javascript from raphael plotting library
There's not really a need for anything dramatic beyond directly drawing onto Cartesian coordinates. Simple heuristics can be used to handle the pathing and likely to hit the optimal minimum number of angles the majority of the time, but and likely the shortest length path even more often. All of this can be done dynamically as you require, but while maintaining precision of graphics without breaking the screen up more discretely that it needs to (pixels should remain the most discrete level) and without the need for complex algorithms.
For the overlay, just set all pixels to the color of your lines and modify the alpha channel bits to transparent or opaque depending on if the pixel is or isn't part of the line. To figure out which bits that are part of the line requires a bit of geometry, but that's a piece of cake once you have everything in place.
To figure out how to draw your line onto the alpha channel, you'll need to figure out the style of your lines. A lot of what you'll do depends on style. A common style is using straight lines that are horizontally and veritcally aligned with quarter circles for right angles.
For the "avoidance" algorithms, these aren't too difficult to implement when you just want to avoid the "boxes" representing your nodes... to decluster all your lines is a bit larger of a task and something that not even Visio employs. To avoid the boxes/nodes, using the midpoint between the edges of the box (such as the vertical edges between geo1 and geo3) is nice to do for symmetry and then picking a simple predefined distance to keep non-connecting lines (that is lines that do not connect to that particular box) away from the boxes also works well. A generalized algorithm for this is simple to do, but a little too verbose to describe here, but is essentially a set of generalized checks and switches working on horizontally and vertically align lines and quarter turns. If you end up wanting more detail on how to do this, just post a comment on this answer.
If you're looking for something that's already made for you, the type of connections and rearranging that you want really depends on the application and not a lot of people make tools that are low in demand or too specific of a demand. Obviously this type of software is out there since Visio and others employ it, but whether or not it's available as open source or part of some other free libraries I'm not certain.