Physics Objects problem...

Hi all,

Is there any way to prohibit the creation of object with other objects. It’s really quite irritating.

Thanks. [import]uid: 68047 topic_id: 11857 reply_id: 311857[/import]

Come again? [import]uid: 58455 topic_id: 11857 reply_id: 43195[/import]

Well, the physics engine, as far as I am aware, allows the spawning of objects with other objects, which causes them to either become stuck or get flung around.

I’ll do a video if it might help?

Thanks. [import]uid: 68047 topic_id: 11857 reply_id: 43196[/import]

A video (and some code) would help a lot :slight_smile:

Not sure if this is the problem, but if you have two physics bodies spawned at or close enough the same location, they’ll collide. Had that problem myself with my slingshot - was spawning the replacement object too soon, and the launched object smacked into it and fell harmlessly to the ground. [import]uid: 58455 topic_id: 11857 reply_id: 43200[/import]

http://www.youtube.com/watch?v=mfAzSwYXk6s

local function spawnCrate(event) local crate = display.newImage("images/crate.png") crate.x = event.x; crate.y = event.y; --crate.xScale = 0.5; crate.yScale = 0.5; physics.addBody( crate, { density=1000, friction=0.5, bounce=0.001} ) end background:addEventListener("tap", spawnCrate)

you need a background image for the listener. [import]uid: 68047 topic_id: 11857 reply_id: 43216[/import]

So what problem are you having - multiple crates spawned per tap?

BTW, I fixed my problem by waiting a second after launch to spawn another. [import]uid: 58455 topic_id: 11857 reply_id: 43229[/import]

I was thinking about timers, but for a different problem. The problem is I think it’s annoying how physics objects, like my crates can be spawned inside other physics objects. Any ideas how to stop the user tapping inside a crate and having another crate appear, because nothing I’ve tried works.

Look at the video and see how when I click inside a crate, another crate is created. It causes movement and problems which I’d rather not have.

Thanks. [import]uid: 68047 topic_id: 11857 reply_id: 43230[/import]

You’re going to have to handle that in your tap listener. Iterate through the crates already onscreen and make sure you’re none are going to intersect with the one you’re about to create.

There are times you might want one physics object inside another hollow or concave object. e.g., a fly inside a bubble, or a kernel of corn inside a popcorn popper, or any number of things. [import]uid: 58455 topic_id: 11857 reply_id: 43273[/import]

Hmm, not entirely sure what you mean. I might copy and paste all my code and link it to here, unless you can explain what “Iterate through the crates already onscreen and make sure you’re none are going to intersect with the one you’re about to create.” means :smiley:

Thanks a bunch though. [import]uid: 68047 topic_id: 11857 reply_id: 43303[/import]

I haven’t tested this, so you might have to modify, but try something like this. Create crateGroup somewhere at the start of your scene/program:

local crateGroup = display.newGroup()  

Then in your handler, loop through crateGroup and check that the tap isn’t touching an existing crate before adding a new one. There might be a better Corona-ish way to check if the tap hits a crate - this is kind of a brute force method.

local function spawnCrate(event)  
 local allClear = true  
  
 for i=1, crateGroup.numChildren do  
 -- Check if the tap hits an existing crate  
 local c = crateGroup[i]  
 if ( (event.x \>= c.x - c.contentWidth/2) and (event.x \<= c.x + c.contentWidth/2) and  
 (event.y \>= c.y - c.contentHeight/2) and (event.y \<= c.y + c.contentHeight/2) ) then  
  
 -- Tap touches an existing crate - don't add a new one  
 allClear = false  
 end  
 end  
  
 if (allClear) then  
 local crate = display.newImage("images/crate.png")  
 crateGroup:insert(crate)  
 crate.x = event.x; crate.y = event.y;  
 physics.addBody( crate, { density=1000, friction=0.5, bounce=0.001} )  
 end  
end   

[import]uid: 58455 topic_id: 11857 reply_id: 43310[/import]