Two problems: PNGs and Physics

Hello

I’ve searched the forums for answers to these questions but haven’t found anything - apologies if there’s something already out there.

I have 2 small problems I can’t figure out.

The first: I have created 4 boxes around the edge of the screen using display.newRect, to create a border/frame. I then added a ball to the centre using display.newCircle, radius 8. I have then turned on physics with gravity 9.8 and turned on the accelerometer.

When the ball hits the bottom box it bounces and stops - perfect. But when the ball touches the side boxes, it passes straight through, even if its travelling very, very slowly.

I tried adding another static box on the floor and this has the same problem, where the ball stops on the top, but passes through the sides.

Any ideas why this happens?

The second problem: I created an image in Photoshop, with a transparent background, 32x32px and I drew a triangular ramp shape inside this image, 32px wide by 16px high. When I add it to the Corona game shown above, the transparent boundary 32x32px is used rather than the shape inside, so the ball rests on top of the invisible box rather than sliding down the ramp.

Any idea how to ignore the background of the image?

Thanks in advance and sorry for the long post!

Steve

[import]uid: 45932 topic_id: 15531 reply_id: 315531[/import]

Hey Steve,

First up, with your triangle - you need to define the shape of the body. See here; http://developer.anscamobile.com/reference/index/physicsaddbody

Scroll down a bit and you’ll see pentagons mentioned; that’s where you want to look :slight_smile:

For the first part, can you share some code? It just ignoring them makes me thinks maybe there’s a little error in how they’ve been created.

Peach :slight_smile: [import]uid: 52491 topic_id: 15531 reply_id: 57380[/import]

Aha! Thanks for the reply Peach!

I’ll try out the polygon and let you know if I have any problems. :slight_smile:

Here’s the code for the other problem:

  
display.setStatusBar (display.HiddenStatusBar)  
  
\_W = display.contentWidth;  
\_H = display.contentHeight;  
  
local physics = require ("physics")  
physics.start()  
physics.setGravity(0,9.8)  
physics.setDrawMode("debug")  
  
local motionx = 0  
  
background = display.newImage ("background.png")  
  
wall\_left = display.newRect(0,0,10,\_H);  
wall\_left:setReferencePoint(display.TopRightReferencePoint);  
physics.addBody(wall\_left, "static",{ density=10, friction=0.3, bounce=0.0});  
  
wall\_right = display.newRect(\_W-10,0,10,\_H);  
wall\_right:setReferencePoint(display.TopLeftReferencePoint);  
physics.addBody(wall\_right, "static",{ density=10, friction=0.3, bounce=0.0});  
  
wall\_bot = display.newRect(0,\_H/2+179,\_W,10);  
wall\_bot:setReferencePoint(display.TopLeftReferencePoint);  
physics.addBody(wall\_bot, "static",{ density=10, friction=0.3, bounce=0.0});  
  
ball = display.newCircle(0,0,8);  
ball:setFillColor(200,200,200)  
ball:setReferencePoint(display.CenterReferencePoint);  
ball.x = \_W/2; ball.y = 16;  
physics.addBody(ball, "dynamic",{density=0, friction=0.3, bounce=0.0, radius=8})  
  
local function onAccelerate( event )  
 motionx = 35 \* event.xGravity  
end  
Runtime:addEventListener ("accelerometer", onAccelerate)  
  
local function moveball (event)  
ball.x = ball.x + motionx  
end  
Runtime:addEventListener("enterFrame", moveball)  
  

I think I actually took the accelerometer bit from Techority - a great website I found! :wink:

Thanks for any more help!
S
[import]uid: 45932 topic_id: 15531 reply_id: 57407[/import]

Hey :slight_smile:

Thanks - I do try to keep some useful stuff on there :wink:

I just tested this (only in the simulator as I don’t have time to build this second) and changing the gravity so the ball hit the side walls, it did not go right through them.

Are you still having the problem? The code appears fine at a glance - let me know and if so, will try to build this later and see what’s up.

Peach :slight_smile:

[import]uid: 52491 topic_id: 15531 reply_id: 57457[/import]

Thanks Peach. I forgot to say it does work fine on the simulator. Its only when I build the project it stops working. [import]uid: 45932 topic_id: 15531 reply_id: 57465[/import]

Thanks for the updated info. I will try and take a look tonight :slight_smile:

Peach [import]uid: 52491 topic_id: 15531 reply_id: 57572[/import]

Hey again,

I had a quick look - code looks fine. I can see that it is going through the walls though, as you said. This great post by Jon Beebe has some tips on that; http://blog.anscamobile.com/2011/08/solutions-to-common-physics-challenges/

Beyond that, I notice you aren’t using bounce - which makes me think the walls are only to keep the ball inside the screen and not to get some kind of visual effect of it bouncing off, or the like - if that’s the case have you considered using something like this; http://techority.com/2010/12/08/how-to-wrap-your-iphone-apps-screen/

Let me know :slight_smile:
Peach [import]uid: 52491 topic_id: 15531 reply_id: 57628[/import]

Hey Peach,

Thanks for looking at this again for me. It is odd isn’t it? I’ve started having a look through Jon’s post… I’m trying a few things out and I’ll see if any of them work.

I have already tried the second point as you suggested. The walls will be part of the final app, so I do need them there ideally - I don’t want the ball to wrap.

I did take it one step further and used a similar function to your wrapping one to stop the ball at 10px from the side of the screen, but it caused some weird effects! The ball goes into a strange “looping bounce”. I’m still thinking this will be the best way to keep the ball from going through the blocks, but I need to perfect it slightly.

I just thought there should be a simpler answer to stop the initial problem!

Will let you know if I get it to work - if you’re interested.

Thanks again
Steve
[import]uid: 45932 topic_id: 15531 reply_id: 57631[/import]

I’m interested and I’m sure others will be as well.

Sadly physics is always going to create challenges here and there and walls (and objects passing through them) seem to be one of the big issues we all face.

It may be work glancing over the simple pool sample code, as that has fast moving physics objects that do not go through the walls.

That article by Jon does have some great stuff in it though and I know it’s sorted a lot of problems out in the past :slight_smile:

Peach [import]uid: 52491 topic_id: 15531 reply_id: 57723[/import]

Hi again,

I tried all the steps in Jon’s post but nothing appears to work.

Back to the drawing board! [import]uid: 45932 topic_id: 15531 reply_id: 58061[/import]

Hey there,

I’m sorry to hear that :frowning:

Did you check out the simple pool example?

Peach [import]uid: 52491 topic_id: 15531 reply_id: 58158[/import]

Yes, I had a look at the pool example. No joy.

I wonder if it might be something to do with the Accelerometer and the way it moves the ball.

S

[import]uid: 45932 topic_id: 15531 reply_id: 58167[/import]

I considered that as well; I’m just trying to find some of my code if I still have it where I rolled a ball around inside some walls as well - I think it worked…

Ah, OK, when I did that I was changing the gravity on tilt rather than moving an object around. Would that work for you or not so much? (It depends on the other objects in play, obviously.)

Let me know,
Peach :slight_smile: [import]uid: 52491 topic_id: 15531 reply_id: 58333[/import]

Hello again,

For the game I need to be able to tilt the ball to the left and right only, and it must be dragged down by gravity.

I suppose it’s the same theory as your HDSS, except that I need a downwards gravity and borders, rather than wrapping the ball.

Is there a better way of using the accelerometer to move the object than the code above (in my first post)?

S

[import]uid: 45932 topic_id: 15531 reply_id: 58340[/import]