Sprite with physics face direction of movement.

Get help using Construct 2

Post » Wed Aug 15, 2012 10:01 am

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,

ThanksBaked2012-08-15 11:03:32
B
5
Posts: 15
Reputation: 737

Post » Wed Aug 15, 2012 10:26 am

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. LinkUbivis2012-08-15 10:40:02
B
42
S
9
G
6
Posts: 437
Reputation: 5,626

Post » Wed Aug 15, 2012 10:40 am

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;
}
B
22
S
7
G
3
Posts: 189
Reputation: 4,270

Post » Wed Aug 15, 2012 11:02 am

[QUOTE=superkew] 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;
}[/QUOTE]

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?
B
5
Posts: 15
Reputation: 737

Post » Wed Aug 15, 2012 11:07 am

[QUOTE=Ubivis] 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[/QUOTE]

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? :/Baked2012-08-15 11:08:09
B
5
Posts: 15
Reputation: 737

Post » Wed Aug 15, 2012 11:11 am

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?
B
22
S
7
G
3
Posts: 189
Reputation: 4,270

Post » Wed Aug 15, 2012 11:20 am

[QUOTE=superkew] 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?[/QUOTE]

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?
B
5
Posts: 15
Reputation: 737

Post » Wed Aug 15, 2012 12:29 pm

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
B
5
Posts: 15
Reputation: 737

Post » Wed Aug 15, 2012 1:04 pm

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.
B
22
S
7
G
3
Posts: 189
Reputation: 4,270


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 25 guests