Removing PhysicsBody from sprite

Removing PhysicsBody from sprite
0.0 0

#1

I’m trying to remove a physicsbody from my sprite after it dies however this line keeps crashing:

player->removeComponent(player->getPhysicsBody());

I will greatly appreciate any help to help solve my problem.


#2

Hi brother!

You should use:

                world->DestroyBody( player->getPhysicsBody( ) );
                player->setPhysicsBody( nullptr);

Remember: the world variable is your b2World*


#3

Thanks for the reply.
However, it says there is no member named DestroyBody. I looked through the docs as well and there was no matches to DestroyBody.

BTW, I’m using cocos2d-x v3.10


#4

Sorry!

“DestroyBody(b2Body* body)” is a Box2d method - a physics engine that comes with cocos2dx.


#5

How would I get the PhysicsWorld from inside my code?
I’ve tried the following but it doesn’t work.

this->getPhysicsWorld()->DestroyBody(player->getPhysicsBody());

#6

Try it:

     player->removeComponent(player->getPhysicsBody( ));
     sprite->setPhysicsBody(nullptr);

Does it work?


#7

I just tried it but it doesn’t work, it just causes the game to crash.


#8

I saw this site and updated to cocos2d-x v3.15 yet the problem still persists.


#9

I created a new project and tested player->removeComponent(player->getPhysicsBody()); and it works. However, in my game it doesn’t. This is the logcat:

--------- beginning of /dev/log/main
F/libc    ( 5578): Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 5593 (Thread-326)
I/DEBUG   (   84): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
I/DEBUG   (   84): Build fingerprint: 'hp/t7h/pine:4.1.1/JRO03H/v1.05.18_user:user/release-keys'
I/DEBUG   (   84): pid: 5578, tid: 5593, name: Thread-326  >>> com.ycorps.bulletun <<<
I/DEBUG   (   84): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 00000000
I/DEBUG   (   84):     r0 00000000  r1 6998f7ec  r2 664a48d0  r3 00000000
I/DEBUG   (   84):     r4 6967bb64  r5 00000000  r6 664994b8  r7 6998f848
I/DEBUG   (   84):     r8 6998fabc  r9 69426451  sl 0000000a  fp 6998f8ac
I/DEBUG   (   84):     ip ffec2c28  sp 6998f848  lr 68f5a2af  pc 68f5a2b0  cpsr 40000030
I/DEBUG   (   84):     d0  3f00000000000000  d1  00000000437f0000
I/DEBUG   (   84):     d2  437f00003f000000  d3  3f00000000000000
I/DEBUG   (   84):     d4  00ffffff437f0000  d5  3f8000004b7fffff
I/DEBUG   (   84):     d6  3f8000003f800000  d7  000000ff437f0000
I/DEBUG   (   84):     d8  416fca0580000000  d9  416fca0555555555
I/DEBUG   (   84):     d10 0000000000000000  d11 0000000000000000
I/DEBUG   (   84):     d12 0000000000000000  d13 0000000000000000
I/DEBUG   (   84):     d14 0000000000000000  d15 0000000000000000
I/DEBUG   (   84):     d16 0000000000000000  d17 0000000000000000
I/DEBUG   (   84):     d18 00000000a8000000  d19 bfc25fc5b75e7f18
I/DEBUG   (   84):     d20 3fb33dc7dd57be6d  d21 bf29e7f8504cec58
I/DEBUG   (   84):     d22 8000000000000000  d23 0000000000000000
I/DEBUG   (   84):     d24 0000000000000000  d25 8000000000000000
I/DEBUG   (   84):     d26 3ff0000000000000  d27 0000000000000000
I/DEBUG   (   84):     d28 0000000000000005  d29 0000000000000000
I/DEBUG   (   84):     d30 0000000000000000  d31 0000000000000000
I/DEBUG   (   84):     scr 60000013
I/DEBUG   (   84):
I/DEBUG   (   84): backtrace:
I/DEBUG   (   84):     #00  pc 004052b0  /data/data/com.ycorps.bulletun/lib/libMyGame.so
I/DEBUG   (   84):     #01  pc 004052ab  /data/data/com.ycorps.bulletun/lib/libMyGame.so
I/DEBUG   (   84):
I/DEBUG   (   84): stack:
I/DEBUG   (   84):          6998f808  d882f292
I/DEBUG   (   84):          6998f80c  664a11dc
I/DEBUG   (   84):          6998f810  664a48a0
I/DEBUG   (   84):          6998f814  d882f292
I/DEBUG   (   84):          6998f818  6998f82c
I/DEBUG   (   84):          6998f81c  6967bb64  /data/data/com.ycorps.bulletun/lib/libMyGame.so
I/DEBUG   (   84):          6998f820  6998f834
I/DEBUG   (   84):          6998f824  69207dc0  /data/data/com.ycorps.bulletun/lib/libMyGame.so (cocos2d::Vector<cocos2d::PhysicsShape*>::front() const+28)
I/DEBUG   (   84):          6998f828  6998f83c
I/DEBUG   (   84):          6998f82c  664a46f8
I/DEBUG   (   84):          6998f830  6998f844
I/DEBUG   (   84):          6998f834  6998f844
I/DEBUG   (   84):          6998f838  6998f910
I/DEBUG   (   84):          6998f83c  664a48d0
I/DEBUG   (   84):          6998f840  df0027ad
I/DEBUG   (   84):          6998f844  00000000
I/DEBUG   (   84):     #00  6998f848  66499240
I/DEBUG   (   84):          ........  ........
I/DEBUG   (   84):     #01  6998f848  66499240
I/DEBUG   (   84):          6998f84c  665c14e0
I/DEBUG   (   84):          6998f850  6998f858
I/DEBUG   (   84):          6998f854  0000000b
I/DEBUG   (   84):          6998f858  00000002
I/DEBUG   (   84):          6998f85c  665c1480
I/DEBUG   (   84):          6998f860  6998f868
I/DEBUG   (   84):          6998f864  d882f292
I/DEBUG   (   84):          6998f868  66499240
I/DEBUG   (   84):          6998f86c  665c14e0
I/DEBUG   (   84):          6998f870  665c1480
I/DEBUG   (   84):          6998f874  664994b8
I/DEBUG   (   84):          6998f878  6998f880
I/DEBUG   (   84):          6998f87c  68f5fc87  /data/data/com.ycorps.bulletun/lib/libMyGame.so
I/DEBUG   (   84):          6998f880  66499240
I/DEBUG   (   84):          6998f884  665c1480
I/DEBUG   (   84):
I/DEBUG   (   84): memory near r1:
I/DEBUG   (   84):     6998f7cc 6998f7e8 6998f7ec 6998f7f0 6998f8b0  ...i...i...i...i
I/DEBUG   (   84):     6998f7dc 6998f804 664992d4 664a11dc 6998f80c  ...i..If..Jf...i
I/DEBUG   (   84):     6998f7ec 664a48a0 664a48a0 d882f292 6998f824  .HJf.HJf....$..i
I/DEBUG   (   84):     6998f7fc 6998f810 6998f824 6998f824 d882f292  ...i$..i$..i....
I/DEBUG   (   84):     6998f80c 664a11dc 664a48a0 d882f292 6998f82c  ..Jf.HJf....,..i
I/DEBUG   (   84):
I/DEBUG   (   84): memory near r2:
I/DEBUG   (   84):     664a48b0 0000000b 0000000b 00000001 79616c70  ............play
I/DEBUG   (   84):     664a48c0 2e327265 00676e70 bf14aa8e 000003d3  er2.png.........
I/DEBUG   (   84):     664a48d0 6966c4b0 00000001 00000227 00000000  ..fi....'.......
I/DEBUG   (   84):     664a48e0 00000000 85febf00 00000000 00000000  ................
I/DEBUG   (   84):     664a48f0 00000000 00000000 00000000 00000000  ................
I/DEBUG   (   84):
I/DEBUG   (   84): memory near r4:
I/DEBUG   (   84):     6967bb44 69679fb8 695469dc 695469e4 695469ec  ..gi.iTi.iTi.iTi
I/DEBUG   (   84):     6967bb54 40119355 6965fc68 6961d0e8 69500f35  U..@h.ei..ai5.Pi
I/DEBUG   (   84):     6967bb64 00000000 00000000 00000000 40124e35  ............5N.@
I/DEBUG   (   84):     6967bb74 40124f4d 4001d2f5 401305fd 4011821c  MO.@...@...@...@
I/DEBUG   (   84):     6967bb84 401127c8 4005ba08 400625d8 401250f9  .'.@...@.%.@.P.@
I/DEBUG   (   84):
I/DEBUG   (   84): memory near r6:
I/DEBUG   (   84):     66499498 00000000 3f800000 3f800000 42b20000  .......?...?...B
I/DEBUG   (   84):     664994a8 445ec2c2 6649da08 6649df68 00000073  ..^D..Ifh.Ifs...
I/DEBUG   (   84):     664994b8 6966e458 00000001 0000027b 00000000  X.fi....{.......
I/DEBUG   (   84):     664994c8 00000000 6649ea00 664993f0 66499528  ......If..If(.If
I/DEBUG   (   84):     664994d8 6649952c 6649952c 00000001 429d1463  ,.If,.If....c..B
I/DEBUG   (   84):
I/DEBUG   (   84): memory near r7:
I/DEBUG   (   84):     6998f828 6998f83c 664a46f8 6998f844 6998f844  <..i.FJfD..iD..i
I/DEBUG   (   84):     6998f838 6998f910 664a48d0 df0027ad 00000000  ...i.HJf.'......
I/DEBUG   (   84):     6998f848 66499240 665c14e0 6998f858 0000000b  @.If..\fX..i....
I/DEBUG   (   84):     6998f858 00000002 665c1480 6998f868 d882f292  ......\fh..i....
I/DEBUG   (   84):     6998f868 66499240 665c14e0 665c1480 664994b8  @.If..\f..\f..If
I/DEBUG   (   84):
I/DEBUG   (   84): memory near r8:
I/DEBUG   (   84):     6998fa9c 6518cf1c 665d7c08 664762f0 665d4b50  ...e.|]f.bGfPK]f
I/DEBUG   (   84):     6998faac 665c1838 6998fc70 69426b49 00000000  8.\fp..iIkBi....
I/DEBUG   (   84):     6998fabc 664762f0 665c1838 665d4b50 665d4b50  .bGf8.\fPK]fPK]f
I/DEBUG   (   84):     6998facc 665c1838 664762f0 69426b9f 6998fb00  8.\f.bGf.kBi...i
I/DEBUG   (   84):     6998fadc 6998fafc 6967bb64 665d7bf8 00000000  ...id.gi.{]f....
I/DEBUG   (   84):
I/DEBUG   (   84): memory near r9:
I/DEBUG   (   84):     69426430 f7f61c0d 1c29fc59 1c301c04 fc5af7f6  ....Y.)...0...Z.
I/DEBUG   (   84):     69426440 1e581c03 1c204183 41a01e44 bd704018  ..X..A .D..A.@p.
I/DEBUG   (   84):     69426450 684bb570 688e69c2 d027429a 22016a04  p.Kh.i.h.B'..j."
I/DEBUG   (   84):     69426460 d001429c bd701c10 429e6983 2e00d001  .B....p..i.B....
I/DEBUG   (   84):     69426470 1c0dd1f8 2e001c04 6d83d009 d0062b03  ...........m.+..
I/DEBUG   (   84):
I/DEBUG   (   84): memory near fp:
I/DEBUG   (   84):     6998f88c 68f5fc61 665be298 6920d0e4 66499240  a..h..[f.. i@.If
I/DEBUG   (   84):     6998f89c 665c1480 6967bb64 6920bea8 6998f8dc  ..\fd.gi.. i...i
I/DEBUG   (   84):     6998f8ac 6920bc78 66499240 665c1400 00000000  x. i@.If..\f....
I/DEBUG   (   84):     6998f8bc 665c1440 66499240 d882f292 00000000  @.\f@.If........
I/DEBUG   (   84):     6998f8cc 665c14d0 665c1440 6749bb28 6998f8ec  ..\f@.\f(.Ig...i
I/DEBUG   (   84):
I/DEBUG   (   84): memory near sp:
I/DEBUG   (   84):     6998f828 6998f83c 664a46f8 6998f844 6998f844  <..i.FJfD..iD..i
I/DEBUG   (   84):     6998f838 6998f910 664a48d0 df0027ad 00000000  ...i.HJf.'......
I/DEBUG   (   84):     6998f848 66499240 665c14e0 6998f858 0000000b  @.If..\fX..i....
I/DEBUG   (   84):     6998f858 00000002 665c1480 6998f868 d882f292  ......\fh..i....
I/DEBUG   (   84):     6998f868 66499240 665c14e0 665c1480 664994b8  @.If..\f..\f..If
I/DEBUG   (   84):
I/DEBUG   (   84): code around pc:
I/DEBUG   (   84):     68f5a290 68fb4798 d0022b02 2b02693b 687bd12b  .G.h.+..;i.++.{h
I/DEBUG   (   84):     68f5a2a0 2392681a 58d3009b f7fd1c18 1c05fdbf  .h.#...X........
I/DEBUG   (   84):     68f5a2b0 3344682b 2314681e 210018fb 1c182200  +hD3.h.#...!."..
I/DEBUG   (   84):     68f5a2c0 ff30f7f9 18fb2314 1c191c28 231447b0  ..0..#..(....G.#
I/DEBUG   (   84):     68f5a2d0 1c1818fb ff38f7f9 681a687b 009b2392  ......8.{h.h.#..
I/DEBUG   (   84):
I/DEBUG   (   84): code around lr:
I/DEBUG   (   84):     68f5a28c 1c102100 68fb4798 d0022b02 2b02693b  .!...G.h.+..;i.+
I/DEBUG   (   84):     68f5a29c 687bd12b 2392681a 58d3009b f7fd1c18  +.{h.h.#...X....
I/DEBUG   (   84):     68f5a2ac 1c05fdbf 3344682b 2314681e 210018fb  ....+hD3.h.#...!
I/DEBUG   (   84):     68f5a2bc 1c182200 ff30f7f9 18fb2314 1c191c28  ."....0..#..(...
I/DEBUG   (   84):     68f5a2cc 231447b0 1c1818fb ff38f7f9 681a687b  .G.#......8.{h.h

#10

When you create player’s physicsbody do playerBody->setName("physics");
at time of removing do like this,
player->removeComponent(player->getComponent("physics"));


#11

I just tried it yet the game still seems to crash.