0 Favourites

Sprite with physics face direction of movement.

  • Hi,

    I have multiple instances of a Sprite with the physics behavior flying around my screen.

    I just want the sprite to face the direction it is travelling in.

    I have tried setting angle of the object to Angle(0,0,Object.Physics.VelocityX,Object.Physics.velocityY) in the Every tick event. I have also tried similar with a For each Object in the Every Tick event.

    Results can be seen in the project file, seems weird to me.

    Project (just click to fire object)

    Any help or suggestions is appreciated,

    Thanks

  • can you please save it as a .capx? in a caproj file, the sprites are not included.

    I think, the image is just facing the wrong position, but I can only tell for sure, when you provide a .capx

    EDIT: Made an example of a sprite that always faces into direction of movement. Link

  • here is what you need:

    Global variables

    int PrevX;

    int PrevY;

    On Every Tick:

    {

    Set angle to the angle between the current X and Y, and the Previous X and Y

    Set PrevX = CurrentX;

    Set PrevY = CurrentY;

    }

  • here is what you need:

    Global variables

    int PrevX;

    int PrevY;

    On Every Tick:

    {

    Set angle to the angle between the current X and Y, and the Previous X and Y

    Set PrevX = CurrentX;

    Set PrevY = CurrentY;

    }

    Won't those global variables be overwriten since I have more than one instance of an object?

    Also isn't the angle between previous and current X,Y exactly the same as velocity X,Y?

  • can you please save it as a .capx? in a caproj file, the sprites are not included.

    I think, the image is just facing the wrong position, but I can only tell for sure, when you provide a .capx

    EDIT: Made an example of a sprite that always faces into direction of movement. Link

    Updated my project file for you.

    And I can't oipen yours as its version is newer than mine. I thought I had the most recent version though? :/

  • hmmmm,

    I might have just rushed to answer your post :)

    But, remember that the point 0,0 is the top left corner of your screen. So you are continuously calculating the angle from there.

    VelocityX should be the magnitude of the velocity in that direction, as with VelocityY. Calculating an angle based on the origin (top left corner) and the magnitude of the velocity is sure to cause some strange results isn't it?

  • hmmmm,

    I might have just rushed to answer your post :)

    But, remember that the point 0,0 is the top left corner of your screen. So you are continuously calculating the angle from there.

    VelocityX should be the magnitude of the velocity in that direction, as with VelocityY. Calculating an angle based on the origin (top left corner) and the magnitude of the velocity is sure to cause some strange results isn't it?

    As I understand it:

    The velocity is just a coordinate position relative to the current position. It can be negative.

    So the position of an object in the next step using velocity is just x+Vx, y+Vy. So if you calculate the angle from 0,0 using Vx and Vy is should be the correct direction?

  • SOLVED

    The problem was GravityPhysics is a family behavior, so it seemed to be getting the wrong velocity values so I used superkews solution using instance variables instead of globals.

    If anyone knows how to target the correct instance of that behavior I would be very greatful.

    Cheers

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • Hey Baked

    I like your solution to get the angle using the velocity. It is definitely more elegant. I'm at my day job, which is a different thought process to game dev, so I do feel a bit slow.

    I think I will try it out in my game and see if it yields the same results.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)
Similar Topics Posts Views Last Post
Unread hot topic
1,941 263,233
FlyingRam's avatar
FlyingRam
Unread hot topic
0 Favourites
[BEHAVIOR] Chipmunk Physics
673 82,484
nelostic's avatar
nelostic
Unread hot topic
0 Favourites
Sprite Font Generator - v3
368 112,295
RetroDaddy's avatar
RetroDaddy