Computer Science Canada Following the mouse... |
Author: | ZeroPaladn [ Thu Sep 14, 2006 12:33 pm ] |
Post subject: | Following the mouse... |
I was wondering how to make lets say... a bullet go in the directoin of the mouse when fired? I'm at a loss on how to do it. I've heard of using trig and the x and y coords of the mouse and the player and use the pythagoream theorm, but im still at a loss. PS. Its been a while everyone. |
Author: | Cervantes [ Thu Sep 14, 2006 1:13 pm ] |
Post subject: | |
Yes, trig and Pythagorean theorem will be needed. What have you got so far? Draw these things out on paper. It's a lot easier to work out the equations there than type them immediately into your code. |
Author: | MysticVegeta [ Thu Sep 14, 2006 1:24 pm ] |
Post subject: | |
Well you will need Pythagoreans for the shortest distance (hypotneuse) between the bullet and the mouse (x, y). And the trig to figure out what angle it should be shot at. It would be a right triangle so you can use your sin, cos and tan no prob |
Author: | ZeroPaladn [ Fri Sep 15, 2006 11:42 am ] |
Post subject: | |
good idea cervantes, ill draw it out, and see what i come up with then. so let me get this straight, i use the pythagorean therom to find the shortest distance between my char and the mouse coords, then figure out the angle of that using trig, then fire off the bullet in said direction? |
Author: | do_pete [ Fri Sep 15, 2006 12:23 pm ] |
Post subject: | |
MysticVegeta wrote: Well you will need Pythagoreans for the shortest distance (hypotneuse) between the bullet and the mouse (x, y).
Why do you need to find the shortest distance? Don't you just need the coordinates of the mouse and character to calculate the angle? |
Author: | BenLi [ Fri Sep 15, 2006 12:33 pm ] |
Post subject: | |
actually, you will only need trig if you actually have to know what angle fired at. See what I said at this silmilar post http://www.compsci.ca/v2/viewtopic.php?t=13473 |
Author: | ZeroPaladn [ Mon Sep 18, 2006 12:12 pm ] |
Post subject: | |
if only i understood the code... DAMN I HATE TRIG! |
Author: | Ultrahex [ Mon Sep 18, 2006 3:40 pm ] | ||
Post subject: | |||
There Is Also This Way Using Getting The Angle... Here is an example program I just wrote cause i was bored and am procrastinating other assignments
As Long as You Understand "SOH, CAH, TOA" you should understand how i am getting to this (Remeber Sin(theta) = y/x and all that. Remeber Don't just take the code, it provides a guideline even though it probably is more then that for some and i bet i will see my code in a turing program soon but that is how life works i suppose. Hope this clears a few things up? possibly :/ Ask Questions if You Would Like. |
Author: | zylum [ Mon Sep 18, 2006 9:39 pm ] | ||
Post subject: | |||
all you need is similar triangles. take a look at the attached image... there are two similar triangles. with similar triangles you can do something like height_1 / base_1 = height_2 / base_2 and solve for an unknown quantity if you have the other three. in our case, we have the hypoteneus of the small triangle (which represents the velocity vector), the base, hieght and hypoteneus of the big triangle (ie where the mouse is relative to the charater). we want the find the components of the velocity vector (dx and dy) which are represented by the base and height of the little triangle. so we would use the formulas dx / speed = (mx - x) / hypoteneus and dy / speed = (my - y) / hypoteneus. rearange to isolate dx and dy and solve. heres a sample code:
|
Author: | zylum [ Mon Sep 18, 2006 10:02 pm ] |
Post subject: | |
sorry, heres the picture: |
Author: | ZeroPaladn [ Thu Sep 21, 2006 12:09 pm ] |
Post subject: | |
Thanks to you both, Zylum and Ultrahex. I understand it now! I liked Zylum's more simplistic method, although you can glitch out Zylum's program (just hover the mouse of the exact centre of the circle, Ultrahex's just points it straight down). Bits to you both! +25 Bits to Zylum and Ultrahex Thanks again guys! |
Author: | Clayton [ Thu Sep 21, 2006 2:50 pm ] |
Post subject: | |
My oh my, zylum, people seem to like giving bits to you dont they, maybe it has something to do with that even 1000 bits sitting there that they dont like? NOTE: zylum is a MOD, his bits are SET |
Author: | zylum [ Thu Sep 21, 2006 4:54 pm ] | ||
Post subject: | |||
maybe they give me the bits in a gesture of appreciation. and since you're so fond the rules, maybe you should follow them and stop spamming. [Rules] anyhoo, to fix the division by zero problem, i usually add a very small value to the denominator. technically, if the denominator is equal to the negative of the value i add, it will also be division by zero but that is far less likely than it would otherwise be..
|
Author: | TheOneTrueGod [ Thu Sep 21, 2006 5:26 pm ] |
Post subject: | |
why don't you just do an "if" check? it'll eliminate any "stupid" things from happening, like the -1e4 value cropping up.. (Though since you used sqrt, this isn't possible in this particular instance, but I have seen a few times where the program you provided COULD result in a division by zero error...) |