valueerror Posted June 19, 2014 Share Posted June 19, 2014 i want to fix the weapon to my player so i did player.addChild(weapon); this is good and fixes the sprite to the player but when i enable the pyhiscs body , disable gravity for the weapon, set it to kinematic and so on - no matter what i do the physics body will stay at it's koordinates... so i did something like weapon.body.x = player.body.x (that fixes the physics body position but then the sprite has a huge offset for some reason.. i want to rotate my weapen so i want the anchor to be at (0.5,1) this works great for the sprite but the body always keeps rotating around 0.5,0.5 (the center) all in all i can say.. nothing works as i want it to work.. please help.. thx! Link to comment Share on other sites More sharing options...
Joe Kopena Posted June 19, 2014 Share Posted June 19, 2014 If you're just trying to attach a weapon decorator to the sprite, you probably don't want to attach physics to the weapon. The main sprite would move about, rotate, collide, etc., and the child sprites are basically just getting drawn on top of it. Link to comment Share on other sites More sharing options...
valueerror Posted June 22, 2014 Author Share Posted June 22, 2014 well thats the problem.. with addChild i can use this pixi function to draw the sprite (and fix it) to the player.. but i need the sprite physics shape to be there too.. because IF some enemies collide with my weapon (or vice versa) the enemies should die.. so this special object (the weapon) needs to have a physics body to detect collisions... Link to comment Share on other sites More sharing options...
OpherV Posted June 22, 2014 Share Posted June 22, 2014 Here are two quick ideas:1. Instead of adding a body to the weapon sprite, you can add a new shape to the character body. It might help you to set its shape as a sensor.2. Don't add the weapon as a child, but rather add it as a sibling of the character and use some constraint to fix the two together Link to comment Share on other sites More sharing options...
titmael Posted June 23, 2014 Share Posted June 23, 2014 Want to do the same thing here, any solution ? I move the coordinates of the weapon in it's update function (related to the player coordinates) but still got some probs : http://www.html5gamedevs.com/topic/7352-p2-do-not-apply-gravity-to-a-sprite/ Link to comment Share on other sites More sharing options...
valueerror Posted June 23, 2014 Author Share Posted June 23, 2014 well .. to sum up my problem : i am able to create a sprite with a pyhiscs body and pin it to my player with a revolute constraint.. the player isn't affected at all because i use different collision groups and the player and the weapon do not collide. further the weapon has no gravityScale, fixedRotation, and is dynamic. so it just hangs there.. i scale it to -1 when the player scales to -1 (looks to the left) everything fine ..BUT.. now i want to "rotate" my weapon (the player should be able to strike) and NO MATTER WHAT .. i can't get it to rotate around another point than the absolute center of the sprite (0.5,0.5) if i set the anchor to (0.5, 1) the sprite suddenly moves up and rotates around the correct point.. but the point didn't move.. the sprite did.. the pysics body rotates around the very same point but stays where it is and therefore still rotates around the center.. i tried.. sprite.body.offset sprite.pivot setting the anchor before enabling the physics body and tried setting it after that.. no change..i didn't realise that you cant set the rotation point for a physics body.. what is this .. a bug ? the only way to get it right is to work with pivot or anchor.setTo (after enabling physics for the weapon) - that moves the sprite up and it rotates around a different point. and then setCircle(x,y, OFFSET) with this offset i get the rotation point of the body to the point i want it to and to function quite like i wanted it to.. but this kinda sucks.. why is anchor behaving so strange? Link to comment Share on other sites More sharing options...
wayfinder Posted June 23, 2014 Share Posted June 23, 2014 Try creating the weapon body at the same position as your player, and offset the shape:this.player = this.add.sprite(100, 100);this.physics.p2.enable(this.player, true);this.weapon = this.add.sprite(100, 100);this.physics.p2.enable(this.weapon, true);this.weapon.body.setRectangle(50, 50, 50, 0, 0);This should create a 50 x 50 square, 50 px to the right of your player. If you rotate this.weapon.body, it will rotate around the player. Link to comment Share on other sites More sharing options...
valueerror Posted June 23, 2014 Author Share Posted June 23, 2014 thx wayfinder for your code - but this is exactly what i tried to describe.. it is what i am already doing but only setting an offset for the rectangle (or circle it doesn't matter) just offsets the pyhiscs body.. that way the sprite stays on the same place.. i have to offset the sprite with the anchor or pivot method too... and that i find weird.. i believe that some time ago setting the anchor moved the physics body too... Link to comment Share on other sites More sharing options...
wayfinder Posted June 23, 2014 Share Posted June 23, 2014 Ahh, okay Sorry Link to comment Share on other sites More sharing options...
titmael Posted June 24, 2014 Share Posted June 24, 2014 Ok I'm gonna try your solution valueerror and will tell you if I find a solution for the rotation Link to comment Share on other sites More sharing options...
titmael Posted June 25, 2014 Share Posted June 25, 2014 Ok very nice, the revolute constraint is working ! I still use body.angle to rotate the weapon, you should try Link to comment Share on other sites More sharing options...
valueerror Posted June 25, 2014 Author Share Posted June 25, 2014 i use body angle too.. it works very well .. i forgot to mention that i use body.mass=0.1; for the weapons too so they wont affect my player while jumping (or at least not very much) .. when changing weapons i just destroy() the current weapon and clear the constraint first and create a new one.. the only thing i find strange is - as i already mentioned - the way to define an offset for the rotation point but i can deal with it ... Link to comment Share on other sites More sharing options...
titmael Posted June 26, 2014 Share Posted June 26, 2014 I just use this.anchor.setTo(.5,1) and it works perfectly Link to comment Share on other sites More sharing options...
valueerror Posted June 26, 2014 Author Share Posted June 26, 2014 hmmm .. Thats weird .. In my case this only moves the sprite and the physics body still has its anchor at 0.5,0.5 Link to comment Share on other sites More sharing options...
titmael Posted June 26, 2014 Share Posted June 26, 2014 Did you set the anchor after activating P2 on the sprite ? Link to comment Share on other sites More sharing options...
valueerror Posted June 26, 2014 Author Share Posted June 26, 2014 i tried it after and before setting up physics.. but just to be sure ill give it another try tomorrow... Link to comment Share on other sites More sharing options...
titmael Posted June 26, 2014 Share Posted June 26, 2014 when you active P2 it'll put the anchor to 0.5, so you have to set it after to get a custom anchor Link to comment Share on other sites More sharing options...
Recommended Posts