  Game Programming and Development Tools

*blawing out of eyes* DAng it! I really thought this equasion would work... – Realm Master

Realm Master Member

Posts: 1971
From: USA
Registered: 05-15-2005
*really incredibly titanic gimungous big-bird sized sigh*

Well friggen nuts. This was supposed to be a really simple 2d physics test, but I can't even do the first part!

Simply put I have two ojbects, ones in teh center, the other is wherever the mouse is. When I click, it creates an ojbect taht is supposed to zoom directly to the ojbect in the center, now all I need is an equasion to make that happen, right? right.

So we all know (or should know) how to find a 2d slope/angle, right? easy. y1 - y2 / x1 - x2 = angle/slope (those are supposed to be sub 1's and 2's) Yay!!! That SHOULD work, correct! And then ot get it sos I can goes like thiss: x = x + vectorx and y = y + vectory should send my little object straing towards the center thingy.

to get my vectorx and vectory I do the following i take the angle/slope and do this.

vectorx = Cos(angle/slope) * ojbectspeed
vectory = Sin(angle/slope) * ojbectspeed

(Even If I got Cos and Sin mixed up there, I tired it both ways in my program and found the correct way but it still didn't work)

So... we have....

y1 - y2
------ = angle
x1 - x2

vectorx = Cos(angle) * objspeed
vectory = Sin(angle) * objspeed

x = x + vectorx
y = y + vectory

In theory, Perfectomundo, so...

WHY IS IT NOT WORKING?!?!!?!?!?!?!?!?!

...

?!?!?!

------------------
yeah, im a little crazy
Check out my crazy sig that I made: HanClinto Posts: 1828
From: Indiana
Registered: 10-11-2004
What about it is not working?

I'm not entirely sure how you're getting your angle, but you need to be careful when dealing with degrees and radians. You may already know this (so if I'm repeating stuff you already know, I apologise), but computers expect sin() and cos() to have radians as inputs.

The conversion ratio between degrees and radians is:

180 degrees = 3.14159... radians

--clint

Realm Master Member

Posts: 1971
From: USA
Registered: 05-15-2005
Please Elaborate, this probelm is driving me nuts.

What i do is aI have two ojbects, to find the "slope" between them (Picture 2d graphs, line graphs you know, you know the equastion....
y(sub)1 - y(sub)2
--------------- = slope_of_line_that_can_be_interpereted_as_the_"angle"_between_the_two_coordinates
x(sub)1 - x(sub)2

x and y sub1 represent the coordinates of teh first ojbect, and likewise x and y sub2 represents the coordinates of the second ojbect.)

What I get from my lidddle equasion is a number very similar to your 3.14159... its just that the things I'm using are behaving very strangly. No matter where I "place" my ojbect that is supposed to use the angle to hit the ojbect in the center, they go off to the right side of the screen, no matter what I do! I'm sorry if I'm so vauge, and I will post source if its that confusing (be warned, I'm a sloppy programmer)

------------------
yeah, im a little crazy
Check out my crazy sig that I made: steveth45 Member

Posts: 536
From: Eugene, OR, USA
Registered: 08-10-2005
quote:
Originally posted by Realm Master:

y1 - y2
------ = angle
x1 - x2

vectorx = Cos(angle) * objspeed
vectory = Sin(angle) * objspeed

This is where you went wrong. What you call the "angle" is actually a slope. They are very different things. You can't take a slope and use it like an angle. This problem of yours needs no sin/cos functions at all.

To get a vector that goes from point 2 to point 1, this is it:

(x1 - x2, y1 - y2)

Now, to scale this vector to a certain speed, you first need to normalize the vector, which makes it's length exactly 1, and then multiply it by the speed you want.

Lets call our non-normalized vector (a, b).
A normalized version would be calculated by finding the length of the vector and dividing each component by that length.

length = square root( a^2 + b^2 )

normalized vector = (a / length, b / length) = (c, d)

Now, just multiply that by the speed you want,

(c * speed, d * speed).

That will give you what you want, sort of. You'll need to take into account making the point stop when it reaches the center. Really the trick is to view the vector as a single unit that is capable of addition and subtraction with other vectors (of similar dimension) and multiplication and division with scalars (single values).

------------------
+---------+
|steveth45|
+---------+

Realm Master Member

Posts: 1971
From: USA
Registered: 05-15-2005
...THANK YOU!!!

Well, its still Christmas morning, so I have to get back with the family, but thanks and I will look all that there math over after We're done.

Thanks a ton...

(and I don't need it to stop)

------------------
yeah, im a little crazy
Check out my crazy sig that I made: Mene-Mene Member

Posts: 1398
From: Fort Wayne, IN, USA
Registered: 10-23-2006
What's the angle then?
 Function Ang(x1,y1,x2,y2) a# = x1 - x2 b# = y1 - y2 length# = Sqr#((a#^2) + (b#^2)) c# = (a / length#) d# = (b / length#) angle = ACos(c) angle = ASin(d) Return angle End Function

Function fixed.
------------------
MM out-
Thought travels much faster than sound, it is better to think something twice, and say it once, than to think something once, and have to say it twice.
"Frogs and Fauns! The tournament!" - Professor Winneynoodle/HanClinto

[This message has been edited by Mene-Mene (edited December 28, 2006).]

steveth45 Member

Posts: 536
From: Eugene, OR, USA
Registered: 08-10-2005
Well, the angle is not important for this problem, but it could be easily calculated. First of all, angles are relative. Most commonly, you could find the angle relative to the horizontal, or (1, 0) vector. Using the normalized vector (c, d), with the angle "theta" from the horizontal, imagine a right triangle where the hypotenuse (length of 1) is the normalized vector. We know the following:

c = cos(theta)
d = sin(theta)

So, to find the angle (theta), you could do one of the following:

theta = acos(c)
theta = asin(d)

Generally, these functions return the angle in radians (0 - PI). Multiply by (180 / PI) to get the angle in degrees. The angle doesn't tell you if the slope is positive or negative. A vector with a slope of 1 and a vector with a slope of -1 will both calculate to a 45 degree angle from the horizontal.

In games, acos and asin are processor expensive functions, that don't give you much more information than you already have. To find relative angles between vectors, you generally normalize the vectors and take the dot product. This gives you cos(angle). So, if the dot product equals 1, then the vectors have identical angles (0 degrees difference), if the dot product is 0, then the angles are orthogonal (90 degrees difference), and if it is -1, they are pointing in opposite directions (180 degrees difference). 1 to -1 is as easy a range to use as 0 to PI or 0 to 180.

------------------
+---------+
|steveth45|
+---------+