
(VB) Making a bot... Get pixels in a circle.
What methods would u use to find the angle from the center of the circle to the red spot?
Let's say that circle is a "ship", and that the red spot is the front of the ship.. I need this to make a bot/AI for a small 2D game called UniBall for finding out wich way the "ship" is heading.
I thought of looping around like a clock, but I don't know how (the mathematics). Can anyone help me?
[edited by - -Fruz- on June 10, 2002 5:14:45 PM]

Assuming you know the (x,y) location of the center of the red spot you could first build a vector between the red spot's center and the center of the circle:
v = (red_spot.x - circle.x, red_spot.y - circle.y)
then find the arc tangent of the vector
arctan(v.y / v.x);
BEWARE! Arctan() is only defined for -Pi/2 to Pi/2, but using C in the math.h header is a function atan2() which will take into account the quadrant of the angle and return the correct angle.
*EDIT Re-reading your post I think I misunderstood the question/picture with the red ball as the ship and the white circle as the AI.
I will not make a list of links... I will not make a list of links... I will not make a list of links...
Invader's Realm
[edited by - Invader X on June 11, 2002 1:42:16 AM]
v = (red_spot.x - circle.x, red_spot.y - circle.y)
then find the arc tangent of the vector
arctan(v.y / v.x);
BEWARE! Arctan() is only defined for -Pi/2 to Pi/2, but using C in the math.h header is a function atan2() which will take into account the quadrant of the angle and return the correct angle.
*EDIT Re-reading your post I think I misunderstood the question/picture with the red ball as the ship and the white circle as the AI.
I will not make a list of links... I will not make a list of links... I will not make a list of links...
Invader's Realm
[edited by - Invader X on June 11, 2002 1:42:16 AM]
June 11, 2002 12:50 AM
dont forget how to say how to use atan2. instead of atan(dy/dx) you write atan2(dy, dx). this is all presuming you have the coodinates of the two things.
by the way you descripe the problem it seems that all you have is the picture and nothing else. in that case i would either change the structure of your program or do some sort of sweep perhaps as you thought of
by the way you descripe the problem it seems that all you have is the picture and nothing else. in that case i would either change the structure of your program or do some sort of sweep perhaps as you thought of
InvaderX, I''m happy for ur post and everything, but that''s not exactly what i''m asking for. To say it like this:
MY goal is to find out WHERE that red spot actually is inside that circle. The only thing that helps me is that i know the color code of the red dot. So I wanna loop insode the circle, checkin pixel by pixel until the pixel turns out RED, then I would know the rolaction of the red spot, and i could figure out wich way that ship is going.
MY goal is to find out WHERE that red spot actually is inside that circle. The only thing that helps me is that i know the color code of the red dot. So I wanna loop insode the circle, checkin pixel by pixel until the pixel turns out RED, then I would know the rolaction of the red spot, and i could figure out wich way that ship is going.
There are ways to this, but it''s not a good idea to program like this. Always remember that the screen is an _output_ device, and while you can certainly check the color of each pixel on the screen, it''s not advised to do so and this method should only be used when there is no other way to solve a problem.
So how would you go about coding this thing then? The answer is to store all your game info in variables. Then use that information to draw all the ships and stuff on the screen. Like I said, the screen should only be used a an output device and not to get game info from.
So how would you go about coding this thing then? The answer is to store all your game info in variables. Then use that information to draw all the ships and stuff on the screen. Like I said, the screen should only be used a an output device and not to get game info from.
I''m doing this because i''m creating a bot for ANOTHER game, not one i''we made. I cant get hold of game variables since it''s another EXE file.. If i could i wouldn''t be doing it like this.
Ok then, we''ll try a different solution...
Hmmm.... since you''re only asking for the mathematics of this I''m going to assume you have access to the area''s data. I''m thinking that using math functions like sin/cos/arctan/etc will actually take longer than just checking each pixel individually moving across each line (since your ship is a circle this would check some pixels unnecessarily).
There are two ways I think you could do this:
1. Start at a corner and work across each line of data in the bitmap. When you find a red pixel stop and return the position. This will give you a rough estimate of the ship''s heading
2. Start at a corner and work across each line of data in the bitmap. When you find the first red pixel save its location and keep going. Save the location of the last red pixel encounter. When you are done you should have two pixels, each should be exactly 180 degrees across from the other and so you could find the midpoint between them and that would be the center of your red circle, and thus would be more accurate than the first method.
As an optimization, so that you dont have to scan the entire bitmap, you could check to see if your last red pixel is more than 1 line width away from the position you are currently looking at on the bitmap - if so, you''re done with all the red assuming there is only 1 red circle.
I''m sure somebody can improve on my ideas here, I''ve never actually tried to do something like this before and have not thought about it much.
I will not make a list of links... I will not make a list of links... I will not make a list of links...
Invader''s Realm
Hmmm.... since you''re only asking for the mathematics of this I''m going to assume you have access to the area''s data. I''m thinking that using math functions like sin/cos/arctan/etc will actually take longer than just checking each pixel individually moving across each line (since your ship is a circle this would check some pixels unnecessarily).
There are two ways I think you could do this:
1. Start at a corner and work across each line of data in the bitmap. When you find a red pixel stop and return the position. This will give you a rough estimate of the ship''s heading
2. Start at a corner and work across each line of data in the bitmap. When you find the first red pixel save its location and keep going. Save the location of the last red pixel encounter. When you are done you should have two pixels, each should be exactly 180 degrees across from the other and so you could find the midpoint between them and that would be the center of your red circle, and thus would be more accurate than the first method.
As an optimization, so that you dont have to scan the entire bitmap, you could check to see if your last red pixel is more than 1 line width away from the position you are currently looking at on the bitmap - if so, you''re done with all the red assuming there is only 1 red circle.
I''m sure somebody can improve on my ideas here, I''ve never actually tried to do something like this before and have not thought about it much.
I will not make a list of links... I will not make a list of links... I will not make a list of links...
Invader''s Realm
If the red circle is always the same size (or close to it), then what you probably want to do is a multi-pass sort of thing. First off, isolate the red component of the image. This is pretty easy, and will result in an image with only the red circle. Next, perform a hough transform (I have some code for it on my web page, tsuraan.icompute.com; it has to be modified, but it''s the right idea) to find the center of the red circle. Now that you have the center of the circle, you can use its coordinates and the atan2 thing to find the angle. Hope that helps.
Well, actuall it doesn''t, but thanx anyway. I don''t think you have understood what my goal is. My goal is to find the location of the red spot, so that i can calculate that angle the ship is going... See?
quote:
Original post by tsuraan
Next, perform a hough transform to find the center of the red circle. Now that you have the center of the circle, you can use its coordinates and the atan2 thing to find the angle. Hope that helps.
-Fruz-, I dont understand how this solution doesn''t help you... tsuraan says that it finds the location of the circle.
I will not make a list of links... I will not make a list of links... I will not make a list of links...
Invader''s Realm
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement