Image Rotation While Moving

Image rotation while moving

Store the original (not rotated) image in the class and store the position of the car in a pygame.math.Vector2 object:

def __init__(self, surf, x, y, speed, angle):
# [...]
self.surf = surf.convert()
self.surf.set_colorkey(black)
self.pos = pygame.math.Vector2(x, y)
# [...]

Chang the position of the car dependent on the direction. The direction vector is set by self.speed (in y direction) and has to be rotated by self.angle. Note that (.rotate()) rotates in counter clockwise direction. The y-axis points downwards, so when w is pressed, then the vector has to be subtracted and when s is pressed, then the vector has to be add to self.pos:

def update(self):

# [...]

dirvec = pygame.math.Vector2(0, self.speed).rotate(self.angle)

if keys[pygame.K_w]:
self.pos = self.pos - dirvec
elif keys[pygame.K_s]:
self.pos = self.pos + dirvec

Rotate the image and update the rectangle. See also How do I rotate an image around its center using Pygame?:

def update(self):

# [...]

self.image = pygame.transform.rotate(self.surf, -self.angle)
self.rect = self.image.get_rect(center = (round(self.pos.x), round(self.pos.y)))

Full code of the class Car:

Sample Image

class Car(pygame.sprite.Sprite):
def __init__(self, surf, x, y, speed, angle):
super().__init__()
self.surf = surf
self.surf.set_colorkey(black)
self.pos = pygame.math.Vector2(x, y)
self.angle = angle
self.speed = speed
self.image = pygame.transform.rotate(self.surf, self.angle)
self.rect = self.image.get_rect(center=(x, y))

def update(self):

keys = pygame.key.get_pressed()
if keys[pygame.K_a]:
self.angle -= 10
elif keys[pygame.K_d]:
self.angle += 10

dirvec = pygame.math.Vector2(0, self.speed).rotate(self.angle)
if keys[pygame.K_w]:
self.pos = self.pos - dirvec
elif keys[pygame.K_s]:
self.pos = self.pos + dirvec

if self.pos.x > 800:
self.pos.x = 0
elif self.pos.x < 0:
self.pos.x = 800
elif self.pos.y > 600:
self.pos.y = 0
elif self.pos.y < 0:
self.pos.y = 600

self.image = pygame.transform.rotate(self.surf, -self.angle)
self.rect = self.image.get_rect(center = (round(self.pos.x), round(self.pos.y)))

See also How to turn the sprite in pygame while moving with the keys

Sample Image repl.it/@Rabbid76/PyGame-CarMovement

Rotating and moving an image in a canvas element?

  1. Translate the context to the point on the canvas that the object should rotate about.
  2. Rotate the context.
  3. Either:

    • Translate the context by the negative offset within the object for the center of rotation, and then draw the object at 0,0, or
    • Draw the image using the negative offset within the object for the center of rotation.

e.g.

ctx.save();
ctx.translate( canvasLocX, canvasLocY );
ctx.rotate( ballRotationInRadians );
ctx.drawImage( ball_img, -ballCenterX, -ballCenterY );
ctx.restore();

Note that if you need absolute speed, instead of saving and restoring the canvas (handling many properties that you didn't change) you can just undo your work:

ctx.translate( canvasLocX, canvasLocY );
ctx.rotate( ballRotationInRadians );
ctx.drawImage( ball_img, -ballCenterX, -ballCenterY );
ctx.rotate( -ballRotationInRadians );
ctx.translate( -canvasLocX, -canvasLocY );

The previous bit of premature optimization has been blindly parroted from someone else; I have not personally benchmarked to verify that it is correct.

Edit: I've added a mocked up working example of this here: http://phrogz.net/tmp/canvas_beachball.html

iOS - Rotate image while moving it

I found the solution. When I move my UIImageView I change its frame changing its y value. So, it's impossible to transform an object while its frame is changing. So, to move my UIImageView I now change its center property and the transform works like a charm!

How keep the image rotated during the reverse movement?

Reason:

The behavior that is seen is expected one based on your @keyframes and the animation-direction setting. When the animation's direction is set to alternate, the UA executes the animation from 0 to 100 for the odd numbered iterations, 100 to 0 for the even numbered iterations.

As per your keyframes, the transform goes from rotateY(180deg) to rotateY(0deg) at 1% of the animation's duration itself and so during the odd numbered iterations you don't see any visible rotation (as duration is pretty small) and it goes from rotateY(180deg) (at 100%) to rotateY(0deg) (at 99%) because of which you don't get to see any visible rotation during even numbered iterations also.

The problem in writing keyframes for forward direction and re-using the same for the reverse (using animation-direction) is that it can be done only when the states are the same for both. In this case, it is not because the element should be in unrotated state during forward movement and should have rotateY(180deg) during the reverse movement.

Solution:

For the element to be seen in its rotated state, the transform must be retained for some time. So, for your case it is better to do away with the animation-direction: alternate setting and write both the forward and reverse motions within the keyframes itself like in the below snippet.

(Note: Since we are writing both forward and reverse motions within the keyframes, you may have to double the animation-duration).

@keyframes fish01 {  0% {    left: 0%;    transform: rotateY(0deg);  }  49.5% {    left: 90%;    transform: rotateY(0deg);  }  50.5% {    left: 90%;    transform: rotateY(180deg);  }  100% {    left: 0%;    transform: rotateY(180deg);  }}body {  background-color: black;}div {  position: absolute;  margin-left: 18%;  margin-top: 3%;  width: 800px;  height: 500px;  border: 5px double #DDDDDD;  border-radius: 1em 1em;  background-image: url("https://i.onthe.io/vllkyt28101smv87bg.349283fe.jpg");}div img:nth-child(1) {  float: left;  position: absolute;  margin: 0px;  top: 20%;  width: 100px;  height: 50px;  transform: scale(1.5, 1.5);  animation-name: fish01;  animation-duration: 10s; /* double of original time */  animation-fill-mode: forwards;  animation-iteration-count: infinite;  animation-timing-function: ease-in;}div img:nth-child(2) {  float: left;  position: absolute;  top: 20%;  left: 60%;}
<section>  <div>    <img src="https://www.hyperone.com.eg/media/catalog/product/cache/4/thumbnail/9df78eab33525d08d6e5fb8d27136e95/f/i/fish_1.png" />    <img src="http://www.pets4homes.co.uk/images/fish_hero.png" />  </div></section>

rotate an imageView While moving it

try rotating imageview with this method

        CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.fromValue = @0.0f;
animation.toValue = @(2*M_PI);
animation.duration = 1.0f; // this might be too fast
animation.repeatCount = HUGE_VALF; // HUGE_VALF is defined in math.h so import it
[self.imageview1.layer addAnimation:animation forKey:@"rotation"];

and moving view with this method

CGRect frameBottomview1 = self.yourview.frame;
frameBottomview1.origin.y = 300;
[UIView animateWithDuration:0.7 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
[self.yourview setFrame:frameBottomview1 ];

} completion:nil];

Sample Image



Related Topics



Leave a reply



Submit