Camera control on a 2d game?

How do you control a camera on a 2d scene? Just want to move the screen and zoom in and out.

function setup()
    parameter.number("x",-1000,1000,0)
    parameter.number("y",-1000,1000,0)
    parameter.number("z",-1000,1000,0)
end

function draw()
    background()
    
    --perspective()
    --camera(x,y,z,x,y,z)
    
    drawStuff()
end

function drawStuff()
    sprite("Cargo Bot:Codea Icon",WIDTH/2,HEIGHT/2)
    fill(127, 127, 127, 255)
    rect(0,0,100,100)
    rect(WIDTH-100,HEIGHT-100,100,100)
    fill(157, 157, 157, 255)
    rect(50,50,100,100)
    fill(255, 0, 0, 255)
    rect(WIDTH-150,HEIGHT-150,100,100)
end

Let me guess: you crashed when using the camera. That’s a glitch with OpenGL trying to figure out the angle, you had the camera trying to look at itself. I found something like camera(WIDTH / 2, HEIGHT / 2, -WIDTH + 100, WIDTH / 2, HEIGHT / 2, 0) looks directly at the screen. Just modify the WIDTH / 2 and HEIGHT / 2 parts to your need. Also, take a look at translate(x, y), pushMatrix(), and popMatrix() in the documentation.

Yeah it crashed, thanks for the help

Somewhat off the original topic, but I was looking at the new Extreme Reality SDK and wondering how hard it would be to either integrate, or reproduce some of the funcationality here that lets the iPad’s camera act like a Kinect.

“Extreme Reality’s Extreme Motion is the only technology to provide full-body, software-based, motion analysis and control to any computing device or operating system via a standard camera. It enables developers to easily add motion experiences to existing games or applications, and to create a wide range of new experiences (applications, games, security solutions and more) that pioneer Natural User Interfaces (NUI) while breaking the physical barriers of current hardware-based technologies. The SDK supports Unity, C++ and C# programming languages for multiple operating systems, including iOS, Windows7 and WinRT.”

@Mark @Ignatz and I were working on motion detection with the iPad’s camera a long time ago. I got a kind-of working kinect API, but we never shared it. I only got to the point of a small demo game with a little guy on the screen that walks when you walk, and jumps (glitchily) when you jump. And by walk I mean run in place, lifting your legs up very high. :slight_smile:

If you want, PM me, and if it’s okay with Ignatz, I can share it with you.

@SkyTheCoder

I’m very interested in that project that you guys made, The only thing I request is, how was it done?

Its very hard, if not impossible to check pixels in codea, let alone track them

@Prynok You can just use image:get().

Usage:

local r, g, b = img:get(x, y)

or

local r, g, b, a = img:get(x, y)

X and Y have to be >= 1 and <= width or height, depending on whether it’s X or Y, or you’ll get an error.


Sampling every pixel in an image takes a LONG time, so we had to sample pixel at a certain interval, like every 8 or 16 pixels.

@Ignatz did the initial work of tracking pixels, and I did the API (including all the kinect stuff) and tweaked the tracking a little bit. He set up a table with all the background colors, made things fade into the background because backgrounds change, and calculated all the basic movement (including swiping up, down, left, and right).

@Prynok - to track motion, you need to firstly have a way of identifying control movements, eg waving a hand. Color is the easiest method, but unfortunately, there are lots of things the same color as hands, eg arms, faces, the wall behind, etc. So ideally you need a distinct color, eg a yellow glove.

Then you can capture the camera image, perhaps 5 times per second, and sample every 10th or 20th pixel. Take the average position of all the yellow pixels (if that is your color) and compare it with the previous position.

^:)^ You guys are the pros of codea

@Ignatz that’s a great idea, but how efficient is it for FPS and use for games?

@Luatee - not very efficient, I know, but Codea can’t be everything. If you want it to be a Wii, then don’t try to program GTA 5 at the same time.

Still, it’s fun to program motion detection. It’s certainly easy enough to rig a burglar alarm or something that tells you what your dog does when you’re out, by snapping pictures of movements. The hard part is detecting deliberate movements and swipes.