When I was 9 years old, I got really bored one day and decided to draw up a video game that I wanted to make someday called Undersea Adventure. It involves catching fish with bubbles, battling various sea creatures (crabs, etc) and upgrading your ship to contain more bubble cannons and such. I have gotten the engine running and I can move and rotate a ship, shoot a bubble, and catch a fish. However, the aiming for the bubble is inaccurate (it's gives it a trajectory if it's in a certain range), and I want to make the bubble travel on a path determined by the location of the ship and the mouse. Here is my code for aiming currently:
Code:
    if sum(mousePressed) > 0 and ticks-bubbleDelay>bubbleDelayAmount/shooters:
                bubble1 = True
                bubbleX1 = shipX-12
                bubbleY1 = shipY-12
                slope = 0
                b1x = 0
                b1y = 0
                if not(mousePos[0] == shipX):
                    slope = (mousePos[1]-shipY)/(mousePos[0]-shipX)
                if abs(slope) < 0.3:
                    b1x = 6
                    b1y = 0
                elif abs(slope) < 1:
                    b1x = 5
                    b1y = 2.5
                elif abs(slope) < 2:
                    b1x = 3.2
                    b1y = 3.2
                elif abs(slope) < 4:
                    b1x = 2.5
                    b1y = 5
                else:
                    b1x = 0
                    b1y = 6
                if mousePos[1] < shipY:
                        b1y *= -1
                if mousePos[0] < shipX:
                    b1x *= -1
                bubbleDelay = ticks

b1x is the ΔX and b1y is the ΔYfor the bubble. Here is the bubble movement code:

Code:
    if bubble1:
        bubbleX1 += b1x
        bubbleY1 += b1y


I am trying to make an algorithm that makes the bubble travel directly toward where the mouse was when it was fired and travel at roughly the same speed in any direction. Any ideas?
Sounds like you can do something with the sum of 2 (complex) vectors.
This sounds kind of dumb, but what is a complex vector? Shock I have only taken Algebra I and half of Geometry I.
A complex number is a sum of a real number (rational or irrational) and an imaginary number (a real number times i, where i is the square root of -1). So a complex number might look like 3 - 6.7i.

I think PT_ suggested complex numbers because they can represent 2D vectors without using components: the real part (3 in the example) represents the x-velocity, and imaginary part (-6.7) the y-velocity.

Complex numbers can be difficult to work with, though. I would suggest finding the difference between the mouse x and the ship x, and doing the same for y. Now you could use those values for velocity, but this has the problem of increasing the velocity the farther away the mouse is from the ship. To fix this, you could find the magnitude of the velocity and divide each component by it.


Code:
b1x = shipX - mousePos[0]
b1y = shipY - mousePos[1]
magnitude = sqrt(b1x*b1x + b1y*b1y)

b1x /= magnitude
b1y /= magnitude
90259025 wrote:
A complex number is a sum of a real number (rational or irrational) and an imaginary number (a real number times i, where i is the square root of -1). So a complex number might look like 3 - 6.7i.

I think PT_ suggested complex numbers because they can represent 2D vectors without using components: the real part (3 in the example) represents the x-velocity, and imaginary part (-6.7) the y-velocity.

Complex numbers can be difficult to work with, though. I would suggest finding the difference between the mouse x and the ship x, and doing the same for y. Now you could use those values for velocity, but this has the problem of increasing the velocity the farther away the mouse is from the ship. To fix this, you could find the magnitude of the velocity and divide each component by it.


Code:
b1x = shipX - mousePos[0]
b1y = shipY - mousePos[1]
magnitude = sqrt(b1x*b1x + b1y*b1y)

b1x /= magnitude
b1y /= magnitude

Thank you so much! That code works perfectly, apart from going in the opposite direction of the mouse (I can fix that later in the program easily though). I understand what you did too, which is nice. Razz

EDIT: Is there something along those lines that I could do to find how many degrees something on the ship (a bubble cannon, to be specific) needs to be angled to point at the mouse?
Well, I am digging this project up again, except this time I will be working in Unity. So far, I have gotten a nice water background, ship movement (but not rotation yet), and bubble shooter aiming. It seems to be running a lot more smoothly, so I think that this engine is much more appropriate for this project.

EDIT: I got the ship rotation to work. I am starting to get the hang of C#.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement