Help with inserting physics body into a new Group

Okay so I’ve been working on making a game with a character that can explore a map and the camera follows it. So I made a 1440 x 960 image and displayed it in the simulator. I then created a square for a simple character and made it a static object (also setting the gravity to 0,0 ) so it stays in the center of the screen at all times.Then I put all the objects but the character into a display group called game. I then (using the lib_analog_stick code -http://developer.anscamobile.com/code/simple-analog-stick-joystick-module) made the display group move in the opposite direction making it look like the character was going through the map.

My troubles start here:

I added another block and made it a static physics body then inserted it into the display group game. But when I played my game the character doesn’t act like it hit anything when it really did.

Am I doing something wrong or have I left anything out?

-Thanks

-Here’s my code-

[code]

–instead of a background image here’s a couple blocks for reference points

local b1 = display.newRect(0,0,20,20)
b1:setFillColor(0,255,255)
b1.x = 500
b1.y = 0
game:insert(b1)

local b2 = display.newRect(0,0,20,20)
b2:setFillColor(0,255,255)
b2.x = 0
b2.y = 500
game:insert(b2)

local b3 = display.newRect(0,0,20,20)
b3:setFillColor(0,255,255)
b3.x = 0
b3.y = 0
game:insert(b3)

local b4 = display.newRect(0,0,20,20)
b4:setFillColor(0,255,255)
b4.x = 500
b4.y = 500
game:insert(b4)
–character

local ship = display.newRect(0,0,20,20)
ship:setFillColor(0,0,255)
ship.x = 160
ship.y = 240
physics.addBody(ship,“dynamic”)
– The non-working obstacle
local block = display.newRect(0,0,20,20)
block:setFillColor(255,0,255)
block.x = 200
block.y = 240
physics.addBody(block,“dynamic”)
game:insert(block)
–heres the joystick (requires lib_analog_stick file)

local StickLib = require(“lib_analog_stick”)

MyStick = StickLib.NewStick(
{
x = screenW*.1,
y = screenH*.85,
thumbSize = 16,
borderSize = 32,
snapBackSpeed = .75,
R = 255,
G = 255,
B = 255
} )

local function move1( event )
MyStick:move(game, -5, false)
end
Runtime:addEventListener( “enterFrame”, move1 )

[import]uid: 113909 topic_id: 27592 reply_id: 327592[/import]

Physics objects in different groups can have issues with collision, as mentioned in the docs. That said I *think* if you insert objects into a group, then those groups into a group, it *might* fix the issue, though I’m not certain as I haven’t played with this kind of thing in awhile. (I recall doing something like this with director some time back.)

Could you try that and let me know if it helps? [import]uid: 52491 topic_id: 27592 reply_id: 112171[/import]

Sorry i haven’t replied back for a while I was on vacation. I tried doing what you said which is i inserted all the objects that are part of the map into a group called map ( I changed group game to map just to make it easier) and then I inserted the ship into another group called player and then I inserted both groups into the new Game group.

No luck there…

Any other suggestions/answers :slight_smile:

-Thanks for the help so far

-Boxie [import]uid: 113909 topic_id: 27592 reply_id: 112905[/import]

I just went back and looked over some of my old code, I thought I might have done like I mentioned above but then saw that in fact what I did was something else entirely.

What I did was I created another physics object - the hero was just an image, no body - and I put it into my map group, made it invisible and then set it to obj.isHitTestable = true so that it could still register collisions.

Now, of course it will move with the map - but I *think* what I did was in the movement function I just kept putting the body back at the same spot as the hero on screen. I can’t remember the exact logic and am still looking to the up to date version of this project but perhaps you could try something like that?

Peach :slight_smile: [import]uid: 52491 topic_id: 27592 reply_id: 112940[/import]

Thanks for the quick response and the suggestion. I’ll test it out once i get home later today and i’ll let you know if it works, and i might just make a simple function for the hero/character. [import]uid: 113909 topic_id: 27592 reply_id: 112957[/import]

i tried it using a new block named coll for the collision of the character and made a function that just says for the coll.x and coll.y to be equal to the corresponding characters coordinates. And the coll is just another same sized rectangle but a physics body that is inserted into the map group but for some reason the map group movement over rides the stay function and the coll block just stays with the map positioning and movement instead of staying in the center of the screen.

Any other suggestions?

-heres my code so far.

[code]
display.setStatusBar( display.HiddenStatusBar )
local screenW = display.contentWidth
local screenH = display.contentHeight

local physics = require( “physics”)
physics.start()
physics.setGravity(0,0)
physics.setDrawMode(“hybrid”)
local map = display.newGroup();
map.x = 0
map.y = 0
local b1 = display.newRect(map,0,0,20,20)
b1:setFillColor(0,255,255)
b1.x = 500
b1.y = 0

local b2 = display.newRect(map,0,0,20,20)
b2:setFillColor(0,255,255)
b2.x = 0
b2.y = 500

local b3 = display.newRect(map,0,0,20,20)
b3:setFillColor(0,255,255)
b3.x = 0
b3.y = 0

local b4 = display.newRect(map,0,0,20,20)
b4:setFillColor(0,255,255)
b4.x = 500
b4.y = 500
–character

local ship = display.newRect(0,0,20,20)
ship:setFillColor(0,0,255)
ship.x = 160
ship.y = 240

local coll = display.newRect(map,0,0,20,20)
physics.addBody(coll,“static”)
– The non-working obstacle
local block = display.newRect(map,0,0,20,20)
block:setFillColor(255,0,255)
block.x = 200
block.y = 240
physics.addBody(block,“static”)

local function stay()
coll.x = ship.x
coll.y = ship.y
print"here"
end
Runtime:addEventListener(“enterFrame”,stay)

–heres the joystick (requires lib_analog_stick file)

local StickLib = require(“lib_analog_stick”)

MyStick = StickLib.NewStick(
{
x = screenW*.1,
y = screenH*.85,
thumbSize = 16,
borderSize = 32,
snapBackSpeed = .75,
R = 255,
G = 255,
B = 255
} )

local function move1( event )
MyStick:move(map, -5, false)
end
Runtime:addEventListener( “enterFrame”, move1 )

[import]uid: 113909 topic_id: 27592 reply_id: 112980[/import]

I’d really like to try running this and see if I can help you find an answer; I’m sure there’s some “workaround” that will do what you want smoothly and I believe this should be close to it.

Would you perhaps be able to provide a copy of your project (remove art and replace with blank squares if you wish) or a similar sample?

I can’t guarantee I can fix it but I’d like to take a look and see if I can assist.

Please let me know :slight_smile:

Peach [import]uid: 52491 topic_id: 27592 reply_id: 113021[/import]

Scrap that - I have something I can send you I think. It isn’t the neatest but I believe it may help.

Can you email me, please? peach[at]anscamobile[dot]com [import]uid: 52491 topic_id: 27592 reply_id: 113025[/import]