omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Rotating Sprites to Face Movement Direction in Scene

    Pythonista
    4
    7
    4780
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • AtomBombed
      AtomBombed last edited by

      So I am making a game, and I am having touch controls (obviously) for the game. I have it to where the spaceship will fly to the location that you tap on. But I can't figure out how to make it rotate to face it's direction of movement.

      Any help?

      1 Reply Last reply Reply Quote 0
      • JonB
        JonB last edited by

        are you having trouble figuring out rotation in general? Or specifically rotating to point at a specific location?

        1 Reply Last reply Reply Quote 0
        • Oscar
          Oscar last edited by

          @AtomBombed If you post code from your failed attempt I am sure someone will help you out. Without the specifics of the situation I can only give you the hint that math.atan2() is a very handy function.

          1 Reply Last reply Reply Quote 1
          • Webmaster4o
            Webmaster4o last edited by Webmaster4o

            Here, I'll try to explain it to someone like me who hasn't taken pre-calculus yet 😉. I'm in 9th grade Geometry, I have no idea where you are but I think you are young.

            Scene works on a cartesian coordinate system. This describes points in space with (x, y) coordinates. A very useful thing for tasks like this is a polar coordinate system. A polar coordinate system describes points like (r, θ).

            What does this mean? r represents a radius, and θ represents an angle. Think of it like everything being on circles. Take the polar point (3, 60). First, we can make an imaginary circle around the origin with a radius of 3. Then, go 60 degrees up on it. That's your point. See the dot on the green arm on this illustration of how it works:

            If you convert the point of the touch to polar coordinates with the center at the spaceship, that will give you the angle that the spaceship needs to face.

            Here's some code. Given the coordinates of your spaceship, a, and the coordinates of the touch, b, this will return the angle which a needs to face in order to point at b. This angle will be so that 0 degrees is pointing straight left.

            import math
            
            def angle(a,b):
                """What angle point 'a' needs in order to face point 'b' """
                x1,y1=a
                x2,y2=b
            
                def cartToPol(x,y):
                    """Convert cartesian coordinates to polar coordinates"""
                    radius = math.sqrt(x**2 + y**2) #Pythagorean theorem, a**2+b**2=c**2
                    theta = math.atan2(y,x) #Wikipedia told me to do this. Don't ask why. This seems to give a theta between pi and negative pi
                    theta += math.pi #Now it's between 0 and 2pi, which is radians
                    #theta *= 180/math.pi # node.rotation is in radians. To get degrees, uncomment this.
            
                    #0 degrees points straight left.
                    return radius,theta
            
                return cartToPol(x2-x1,y2-y1)[1]
            
            1 Reply Last reply Reply Quote 0
            • AtomBombed
              AtomBombed last edited by

              @Webmaster4o I'm actually a 9th grader, too. You'd be surprised. I learn fast. Thanks for the post.

              1 Reply Last reply Reply Quote 1
              • Webmaster4o
                Webmaster4o last edited by Webmaster4o

                @AtomBombed as a curious person, you can learn a lot by reading https://en.wikipedia.org/wiki/Polar_coordinate_system. It's pretty clear. I was able to understand just by ignoring everything that I didn't understand 😆

                1 Reply Last reply Reply Quote 0
                • AtomBombed
                  AtomBombed last edited by

                  @Webmaster4o alright thanks. Appreciated!

                  1 Reply Last reply Reply Quote 1
                  • First post
                    Last post
                  Powered by NodeBB Forums | Contributors