Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Moving sprites using vectors

edited April 2013 in General Posts: 4

I'm a newbie when it comes to vectors. I want to do something fairly simple. I have a sprite at position vec2 ( x, y ), the sprite has a speed (distance moved per frame) and an angle. So I want the sprite to move in a straight line at the correct angle and speed. How to I calculate the vec2 coordinates for each frame?

Tagged:

Comments

  • BriarfoxBriarfox Mod
    Posts: 1,542

    Look into tween

  • BriarfoxBriarfox Mod
    Posts: 1,542

    I didn't write this, but this helped me when I was starting out

    -- tween testing
    
    -- Use this function to perform your initial setup
    function setup()
    
        -- since we need to tween objects or tables 
        -- let's create a table that will store our values to tween
        -- this object might be your own sprite class or animation class
    
        -- this contains our starting values
        tweenObject =  vec3(WIDTH/2,HEIGHT/2,0)--{x = WIDTH/2,
                       --y = HEIGHT/2,
                       --rotation = 0}
    
        -- here we are actually creating the tween    
        -- the first value is the number of seconds the tween will take
        -- the second parameter is our tween object we just created.
        -- now we pass it a list of "ending values"
        -- we can also pass it our easing type and looping type as well as a 
        -- callback function which I'm not using
    
        -- the ending values
    
        tween(2,tweenObject, {x = WIDTH/4,
                                y = HEIGHT/4,
                                z = 360},
                           {easing = tween.easing.quintInOut,
                            loop = tween.loop.pingpong})
    end
    
    -- This function gets called once every frame
    function draw()
        -- This sets a dark background color 
        background(40, 40, 50)
    
        -- This sets the line thickness
        strokeWidth(5)
    
        pushMatrix()
    
        -- here we are using our tween Object to control
        -- the translation and rotation
        -- during each call to draw our tween values are slowly updated from
        -- start values to end values 
        -- tween means "the in-between values"  
       translate(tweenObject.x,tweenObject.y)
        rotate(tweenObject.z)
    
    
        sprite("Platformer Art:Block Special")
    
        popMatrix()    
    end
    
    
  • IgnatzIgnatz Mod
    edited April 2013 Posts: 5,396

    Tween is fine as long as you can specify an end point. But if you might have collisions or other events, this is probably what you want instead

    Given

    speed=s pixels per second

    angle=a degrees

    x,y = current position

    then new position is

    x=x + s x DeltaTime x sin(angle x pi/180)

    y=y + s x DeltaTime x cos(angle x pi/180)

    DeltaTime is a Codea function giving you the time elapsed since last redraw

    Note that by expressing speed as pixels per second, and using elapsed time to determine how far to move, you ensure smooth animation even if your frame rate is a bit choppy.

  • Excellent thanks, this is what I was looking for. (not so much to write efficient code but to learn and understand).

  • Posts: 10

    Ignatz, how do you specify the speed and angle in Codea? This would be extremely useful to me as well

  • IgnatzIgnatz Mod
    Posts: 5,396

    I have some tutorials on moving stuff around the screen, starting here

    http://coolcodea.wordpress.com/2013/03/11/3-moving-an-object-around-the-screen/

    If you are going to manage the movement yourself, then you have to figure out the x and y change at each redraw. If you use the built in physics engine, it will do that for you, given angle and velocity. My tutorials cover that, too.

  • Posts: 502

    @Ignatz, that reminds me of my first computer manual, with a bouncing ball. :)

    http://www.lemon64.com/manual/manual/6_8.html

Sign In or Register to comment.