Physics with Masks.

For exercise, open up the “Xray” sample.

In that sample, you’ll see  

[lua]local mask = graphics.newMask( “circlemask.png” )[/lua]

Let’s say I have a very original circle, it’s the best circle ever (rawr!)

[lua]local circle = display.newCircle(360,500,20)

physics.addBody (circle, {radius = 20})[/lua] --Assume physics are present

Is it possible to place that circle mask around the circle shape, and have it use physics?

Uses: A character running through a dark cave, and only the immediate area is lit. It’s common, very common to see that in games.

In the xray example, it’s paper, and the mask shows what’s behind the paper. All I am trying to do is apply physics to an object and attach a mask.

I can’t figure it out, I’ve tried and tried and it appears maybe I’m out of luck?

Thanks in advance!

-Nick

Try adding this (very rough) code to the end of x-ray:

local physics = require("physics") physics.start() physics.setGravity(0,1) --physics.setDrawMode( "hybrid" ) local circle = display.newCircle(360,500,20) physics.addBody (circle, {radius = 20}) circle.x0 = circle.x - image.maskX circle.y0 = circle.y - image.maskY local function onEnterFrame( event )     image.maskX = circle.x - circle.x0     image.maskY = circle.y - circle.y0 end circle.enterFrame = onEnterFrame Runtime:addEventListener( "enterFrame", circle )  

Is that the kind of thing you’re looking for?

That’s EXACTLY what I was looking for.  I didn’t think about enterframe, or perhaps I did and didn’t think about updating on every tick so to speak. 

Well, that does solve the issue. Pretty damn simple too!

Thanks Roaming. I like your templates!

-Nick

Try adding this (very rough) code to the end of x-ray:

local physics = require("physics") physics.start() physics.setGravity(0,1) --physics.setDrawMode( "hybrid" ) local circle = display.newCircle(360,500,20) physics.addBody (circle, {radius = 20}) circle.x0 = circle.x - image.maskX circle.y0 = circle.y - image.maskY local function onEnterFrame( event )     image.maskX = circle.x - circle.x0     image.maskY = circle.y - circle.y0 end circle.enterFrame = onEnterFrame Runtime:addEventListener( "enterFrame", circle )  

Is that the kind of thing you’re looking for?

That’s EXACTLY what I was looking for.  I didn’t think about enterframe, or perhaps I did and didn’t think about updating on every tick so to speak. 

Well, that does solve the issue. Pretty damn simple too!

Thanks Roaming. I like your templates!

-Nick