Howdy, Stranger!

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

Move camera with 2d object

edited July 2013 in General Posts: 20

Hi all,

How can I get the scene 'camera' to follow an object as it moves around.

I've thought about just trying to move a sprite background but not sure that's the answer here?

Probably really simple?!

Comments

  • edited July 2013 Posts: 175

    Moving the background would be one approach but I think what you're looking for is the translate(x,y) function :)

    Edit: Note it's cumulative so translate(20,0) translate(30,0) is the same as translate(50,0). But you can pass positive and negative values ;) so translate(50,0) translate(-50,0) is the same as
    translate(0,0).

  • Posts: 1,976

    Also, use resetMatrix() to set the transform back to 0, 0, and pushMatrix() and popMatrix() work like pushStyle() and popStyle().

  • Posts: 20

    Here is my rough code.

    -- physics
    
    -- Use this function to perform your initial setup
    function setup()
        w1 = physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))
        w2 = physics.body(EDGE, vec2(0,0), vec2(0,HEIGHT))
        w3 = physics.body(EDGE, vec2(WIDTH, 0), vec2(WIDTH, HEIGHT))
    
        ball_diam = 60
        p_ball = physics.body(CIRCLE, ball_diam/2)
        p_ball.gravityScale = 0
        p_ball.restitution = .8
        p_ball.friction =0
       -- p_ball.linearVelocity = vec2(math.random(400), math.random(400))
        p_ball.x = 100
        p_ball.y = 200
        vel = "up"
        hang = 0
        bgy = 100
    end
    
    -- This function gets called once every frame
    function draw()
        -- This sets a dark background color 
        background(200,200,200,255)
        sprite("SpaceCute:Background",p_ball.x,bgy,WIDTH*3,HEIGHT*3)
    
        pushStyle()
        fill(255,0,0,255)
        if (vel ~= "down" or ball_diam<10) then 
            ball_diam = ball_diam + 1.5
            vel = "up"
            bgy = bgy + (1*2.5)
            if (ball_diam>=100) then vel = "float" end
        end
        if (vel== "float") then
            hang = hang + 2
            ball_diam = 100
            if (hang>=101) then
                vel = "down"
                hang = 0
            end
            bgy = bgy
    
        end
        if (vel == "down") then 
            ball_diam = ball_diam - 1.5
            vel = "down"
            if (ball_diam<2) then vel = "up" end
            bgy = bgy + (1*1.4)
        end
    
        p_ball.y = p_ball.y + 1.5
    
        ellipse(WIDTH/2, HEIGHT/2, ball_diam)
    
        popStyle()
    
    end
    

    I tried the translate code before posting here, but it didn't work...? Ill have to look up the matrix stuff as I don't understand it.

    This code is a top down view of a ball rising, floating and falling back down to earth for a 2d golf idea I have.

  • Posts: 80

    This is kind of on topic. I am animating 2d textures rectangles. I have 200 rectangles in one mesh in one mesh. I am using setRec to change the postions. The reason behind this is less meshs means less draw calls, or would it be better to use a mesh for each object and use translate to move them. Also, not all rectangels are moved each frame

  • Posts: 455

    @Thwapp it depends how the performance works out. Moving vertices each frame, even if it's just a subset probably means the full vertex buffer has to be transfered to the GPU each frame, and this can be expensive. However, a seperate mesh:draw call for each rectangle with translates is a lot of calls. You probably need to benchmark it each way and figure out which is best for you.

    Also if they move in a somewhat predictable way, eg if you were using it for several layers with parallax type scrolling you could have a mesh for each "layer" and then translate groups of rectangles...

    Alternately if they move in a very predefined way you could get a shader to do the movement such as the shader particle system I build a while back, where you state the start and end positions for a rectangle when it's added to the mesh, and the shader interpolates it's position over time.

    This kind of optimisation is very context dependant.

  • edited July 2013 Posts: 80

    Thas is what I thought. I'll do some tests and I was just looking for a NO never, don"t do that! :)

Sign In or Register to comment.