Moving sprites using vectors

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?

Look into tween

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})

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)

    -- This sets the line thickness


    -- 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"  

    sprite("Platformer Art:Block Special")


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


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).

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

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

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.

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