Jump to content
  • Advertisement
Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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 this post

Link to post
Share on other sites
heres what ive got so far:

angle_range = 10

turret_pos = vec2d(self.rect.centerx, self.rect.centery)
target_pos = vec2d(self.target.rect.centerx, self.target.rect.centery)
dist = y - x

dist_x = dist[0] # gets the x from the dist tuple
dist_y = dist[1] # gets the y from the dist tuple
radians = 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 this post

Link to post
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 this post

Link to post
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 this post

Link to post
Share on other sites
Ok, so im trying to use this but the angle is not getting converted into a vector correctly

x = math.cos(degrees)
y = math.sin(degrees)
print degrees
print (x,y)

heres the output:

(-0.44807361612917013, -0.89399666360055785)

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

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!