# random 2d vector within set range

This topic is 3075 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Im working on a tower defense game in python/pygame and im trying to program a tower that will pick a random direction to shoot but still shoot in the direction of its target.

The towers shoot just fine using 2d vectors but i would like to add some randomness to their shots. And i just dont know how to go about doing this. One thing i thought of was to convert the vector to an angle and then add/subtract a number from that angle which i would then convert back into a vector as a direction for the tower to shoot. But i cant seem to get this working. I would appreciate any help with my method or maybe another method that would work better.

I need the final vector to still be in the general direction of the starting vector and i would like to be able to control the range of the difference between the two with a variable.
Thanks tons for anyone who can help me.

##### Share on other sites
Show what you've tried.

##### Share on other sites
heres what ive got so far:
angle_range = 10turret_pos = vec2d(self.rect.centerx, self.rect.centery)target_pos = vec2d(self.target.rect.centerx, self.target.rect.centery)dist = y - xdist_x = dist[0] # gets the x from the dist tupledist_y = dist[1] # gets the y from the dist tupleradians = math.atan2(dist_y,dist_x)degrees = math.degrees(radians)degrees += (random.random() * angle_range) * random.choice([-1,1])

this converts the vector into a degree value
then gives me a random degree value within the
correct range but it does not convert it back into
a vector
(I have not figured out how to do that correctly yet)

##### Share on other sites
Well, you have a distance and an angle value pointing to the target, or slightly next to it. If you assume your turrent to be the origin of a coordinate frame, you can easily get the (cartesian) coordinates of a point given angle and distance using polar coordinates:

x = distance * math.cos(alpha)y = distance * math.sin(alpha)

See: Polar coordinate system

##### Share on other sites
I wouldn't use degrees in the computations. If you want to be able to specify angle_range in degrees, just do:
  double const pi = std:atan(1.0)*4.0;  double const radians_per_degree = pi/180.0;    double angle_range = 10 * radians_per_degree;

##### Share on other sites
Ok, so im trying to use this but the angle is not getting converted into a vector correctly
example:
x = math.cos(degrees)y = math.sin(degrees)print degreesprint (x,y)

heres the output:

-90.0(-0.44807361612917013, -0.89399666360055785)

that vector should be (0,1) (straight up)
so what do i need to change?

##### Share on other sites
math.cos() and math.sin() both take their arguments in radians.

##### Share on other sites
Ok thanks alot guys everything is working great!

1. 1
2. 2
3. 3
Rutin
23
4. 4
5. 5
khawk
14

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633654
• Total Posts
3013168
×