Advertisement

Distance Between Points (Range Question)

Started by August 30, 2015 01:39 PM
12 comments, last by Aardvajk 9 years, 5 months ago

The math is indeed linear algebra, vectors to be more specific.

You might find these lessons useful:

https://www.khanacademy.org/math/linear-algebra/vectors_and_spaces

The issue you describing seems to be a float to int conversion issue. Unfortunately you still haven't told us anything about the programming language you're using or what environment your working in. For instance, I can write for you all the C code I like, if you're not using C, it won't be of use.

I'm gonna take a look at that link. I'm not using a coding language. My first post I said I was using Construct 2 and showed a picture of the events I have laid out. Construct is event driven, kind of like click team stuff. It's just made by a competitor. It has expressions like cos, tan, random. random returns a random number in a range, like random(1, 100) will return a number from 1-100.

so the formula I made from your first post was:

X = sin(random(0, 360)) * random(1, 100);
y = cos(random(0,360))) * random(1, 100);

this would make X = sin of a random degree multiplied by a random number from 1-100

Y would be = cos of a random degree multiplied by a random number between 1 and 100.

Advertisement

Wait, see if I made a mistake. I did this.

X = sin(random(0, 360)) * random(1, 100);
y = cos(random(0,360))) * random(1, 100);

but the spirte always shoots off up into the top left corner of the screen instead of moving within it's small 100px movable space.

If you are trying to follow here the suggestion of Rattenhirn, just use his formulas as they were presented (in compact form below):

angle = random(0, 2*PI); // assuming radians, if your programming language uses degrees then it's obviously random(0, 360)

length = random(90, 110); // roughly 100 px, you can also have exactly 100 px

dirX = sin(angle)*length;

dirY = cos(angle)*length;

In particular, note how he passes the same angle value to sin() and cos(), instead of generating two different random values to pass to each.

I highly recommend you learn about vectors. Best thing I ever did.

This topic is closed to new replies.

Advertisement