Accelerometer and Physics? *Coding Issue*

Hi Guys,

I'm making a game right now and I have no idea how to code this. My goal is the cloudrect's will be the walls, and the instance2 "which is a pig" will not be able to go through the walls. Now heres the part I don't get, for the "instance2" I'm using the Accelerometer. So how do I make it so that when the Pig hits the wall, the pig doesn't go right through it?

If anyone could help me I would really appreciate it!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
local pig_1 = sprite.newSpriteSheet( "annoyingpigsprite.png", 97, 100 )
 
local spriteSet2 = sprite.newSpriteSet(pig_1, 1, 4)
sprite.add( spriteSet2, "man", 1, 4, 1000, 0 ) -- play 15 frames every 200 ms
 
local instance2 = sprite.newSprite( spriteSet2 )
instance2.x = _W/1.3; instance2.y = _H/2
 
instance2:prepare("man")
instance2:play()
 
clouds = display.newImage("clouds.png")
clouds2 = display.newImage("clouds.png")
 
clouds.x = _W/2; clouds.y = _H/5 -30
clouds2.rotation = 180;
clouds2.x = _W/2; clouds2.y = _H/1.1
        
local cloudrect = display.newRect(_W/8 - 150, _H/1.02, 700, 1)
local cloudrect2 = display.newRect(_W/8 - 150, _H/9 -30, 700, 1)
        
physics.addBody(cloudrect2, "static")
physics.addBody(cloudrect, "static")
        
local physics_body = {};
        physics_body["instance2"] = 
                                {
                                        density = 2, friction = 0, bounce = 0, 
                                        filter = { categoryBits = 1, maskBits = 65535 },
                                        shape = {   22.5, 2.5  ,  14, 15.3 ,  -8.5, 20  ,  -20, 7  ,  -6, -20  ,  30, -24  }
                                }  
                
        physics.addBody(instance2, "static", (physics_body["instance2"]))
         
        instance2.x = _W - 430
        instance2.y = _H * .5
        instance2.xScale = .7; instance2.yScale = .7;
 
local function onAccelerate( event )
        instance2.y = instance2.y + (event.xGravity * 10)
         
end
 
Runtime:addEventListener ("accelerometer", onAccelerate);

the filter settings seems to be wrong use the following setting

for instance2

1
2
filter={ categoryBits = 1, maskBits = 6 }
--collides with categoryBits 2 and 4 (2+4=6)

Thanks so much for your advice. I tried it out but unfortunately it didn't work. It is not a problem with the category bits. That is just for a custom physics object. My problem is with the accelerometer. The cloud rects are walls in my game. You then tilt the phone and the pig can go two different directions. I want the pig to stop when it hits the wall, but it just keeps going through it. That's why I set up the physics objects, to try to stop the pig from going off screen. Do you know how to fix this?

Regards,
Jordan Schuetz
Ninja Pig Stuidos

Ok will try something else. here your instance2 object is a static body and you are manually moving the object on accelerometer event.
right ? can you just try bounding the movement in onAccelerate function. just use an if condition before moving the object.
something like

1
2
3
4
5
local function onAccelerate( event )
   if y > somevalue and y < somevalue then
        instance2.y = instance2.y + (event.xGravity * 10)
   end
end

Yeh that worked! Somewhat...

1
2
3
4
5
6
7
local function onAccelerate( event )
        if instance2.y < _H/1.02 and instance2.y > _H/9 -30 then
        instance2.y = instance2.y + (event.xGravity * 10)
         end
end
 
Runtime:addEventListener ("accelerometer", onAccelerate);

I actually got something to work! The pig just keeps wrapping around the screen. This is just what I wanted. I'm sorry if I wasted any of your time. You do not know how much I appreciate it, I would have never figured out my problem if you had not commented and helped me out. Hope to see you at the Ansca meetup this August 13th in Palo Alto.

Regards,
Jordan Schuetz
Ninja Pig Studios

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
local function onAccelerate( event )
        --if instance2.y < _H/1.02 and instance2.y > _H/9 -30 then
        instance2.y = instance2.y + (event.xGravity * 10)
        -- end
end
 
local function wrap (event) --Function used to keep the player on screen
 
if instance2.y > _H/1.02 + 20 then
        instance2.y = _H/9 - 59
end
 
if instance2.y < _H/9 - 60 then
        instance2.y = _H/1.02 + 20
end
 
end
 
Runtime:addEventListener ("accelerometer", onAccelerate);
Runtime:addEventListener("enterFrame", wrap)

Happy to know that you got it working.. looking forward to see your game soon in the app store..

and about the meetup.... I really really really wanted to attend it but unfortunately I cannot as am from Singapore... The world is not that small.... :(

Ill post my app on the forums once I finish. I'm only doing Android right now. Thanks for everything.

views:1572 update:2011/10/10 9:00:20
corona forums © 2003-2011