c++ - How do you rotate a sprite around its center by caclulating a new x and y position? -
I am using dark GDK and C ++ to create a simple 2d game. I am moving an object, but it revolves around the top left corner of the phantom.
I have the following variables available:
- PLAYER X
- PLAYER Y
- PLAYHEAD
- Rotateangle (360> x> 0)
Is there an algorithm that will modify the axis point of the phantom, is better for the center?
Here is a small code sample:
void player :: move (zero) {If (checkLeft ()) {PlayerX - = PlayerSpeed; If (player x <0) playerx = 0; } If (checkwrite ()) {PlayerX + = PlayerSpeed; If (playerx> 800 - playerwidth) playerx = 800 - playerwidth; } If (checkup ()) {PlayerY - = PlayerSpeed; If (player Y & lt; 0) Player Y = 0; } If (checkdown ()) {PlayerY + = PlayerSpeed; If (Player Y> 600 - PlayerHeight) Player Y = 600 - PlayerHeight; } RotateAngle + = 5; If (rotate angle> 360) rotate angle - = 360; DbRotateSprite (Handle, RotateAngle); DBSpritata (1, PlayerX, Player Y, Handle); }
edit
I am thinking of opening some reputation for this question, I have been given an answer yet Which works for me If a real code can provide samples that work, I will be very happy.
The problem with Blinde's answer is that there is no question that I translate it in front or back, the soul still rotates around the top left hand corner and it is in the top left corner Wandering around roaming around, then it is moving forward, does not get anything back in the same state. The belief that I have on this is:
Just so much The illusion is not that I have created an image of what is happening. The left shows what is really going on and what exactly I need to do.
The answers have so far been described how but I'm afraid that the Dark GDK API is a simple way to think it's too old to be able to do it in a simple way.
Unfortunately, dbRotateSprite
will rotate the phantom at the top left without regard to the result of the sprite, that is why you have no luck with other suggestions. To simulate the rotation of the center, you have to correct the condition of the phantom manually, that is, you only need to rotate the phantom and then take it as a two-stage process.
I am not familiar with the API and I do not know whether Y is measured or down and the way the angle is measured so I am going to make some assumptions if y has many other 2D graphics systems , And angle is increased by the x-axis because it goes from positive x-axis to positive y-axis, so I believe the correct psuedo-code will look like
// PlayerX and PlayerYype The rit represents the center. 1. RotateAngle is a complete rotation, i.e. relative, incremental rotation rotate angle + = 5; Rotate angle% = 360; RadiansRotate = (Rotate * PI) / 180; DbRotateSprite (handle, rotateangle); HalfSpriteWidth = dbSpriteWidth (handle) / 2; HalfSpriteHeight = dbSpriteHeight (handle) / 2; Sprite X = PlayerX - HalfPropitivod * COS (radiosarotate) + Half PrakashHight * sin (radiansotate); Sprite = Player Y - HalfSpotlight * Cos (radiansotate) - Hafsprotivad * sin (radiansotate); // Sprite on the left (Sprite X, Sprite) DBSpritata (1, Sprite x, Sprite, Handle);
Comments
Post a Comment