Jump to content

best way to fix weapon to player?


valueerror
 Share

Recommended Posts

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

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

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

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

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

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

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

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...