3

I am new to flame engine and its capabilities.

I have two components:

  1. player component
  2. gun component

The idea is for the gun component to follow the player when the player moves. In terms of aim gun, how would I go about rotating the gun component around the player component?

Similar to the game Nuclear Throne, see reference below:

enter image description here

mkrieger1
  • 19,194
  • 5
  • 54
  • 65

1 Answers1

3

So it seems you don't want to really rotate the gun component around it's own axis at all, if you want it like in the gif. So you want to rotate the gun position around the local center of the player.

If you have your gun component as a child of the player all positions are relative to the player and 0,0 is in the upper left corner of the player, and you want to rotate it around the center of the player.

To get that point you would do (calculate this in for example onLoad so that you don't create unnecessary Vector2 objects in the update-loop:

final localCenter = player.size / 2;

And then to get the position of the gun you first have to decide the length that you want it to be from the player, this can of course be changed dynamically like in your example.

final length = 100;
final gun.position.setValues(length, 0); // This means that the zero angle is straight up
gun.position.rotate(yourAngle, center: localCenter);
spydon
  • 9,372
  • 6
  • 33
  • 63
  • 1
    Wow, I didnt think I'd get a response form the creator himself! thank you spydon! this work nicely! – Abdurraheem Abdulhakeem May 10 '23 at 03:13
  • No worries! You can accept the answer by pressing the checkmark underneath the scoring. :) – spydon May 10 '23 at 09:08
  • Hi spydon. Im using the gun.position.rotate(yourAngle, center: localCenter); in the update-loop. and Im using the a joystick radAngle to get the angle for the rotate function. But see the gun keeps rotating really fast instead of remaining in the angle of the joystick radAngle. Any idea on how to fix? – Abdurraheem Abdulhakeem May 10 '23 at 19:07
  • 1
    Are you doing setValues(length, 0) in each tick too? Because that is needed for the rotation to become absolute and not relative to its last rotation. – spydon May 10 '23 at 19:54
  • Yes that worked!! sorry for the bother. Thank you so much! – Abdurraheem Abdulhakeem May 10 '23 at 20:14
  • 1
    No worries, I can recommend joining our Discord server for even quicker help. :) – spydon May 11 '23 at 08:37