Best practices when it comes to angles

Discussion and feedback on Construct 2

Post » Mon Jul 31, 2017 2:04 pm

Hi everyone,

I would like to know the best way to refer to angles when I need to check if an object or behavior angle is within a certain range.

As an introduction, I noticed that when I put an object's angle or the movingAngle expression of a moveto behavior into a variable, the result is a variable ranging from 0 to 359.999

When I do the same with the angleOfMotion expression of a Bullet behavior, the result is an angle ranging from -179.999 to 180.

That alone is quite complicated to deal with, as I use both MoveTo and Bullet behaviors in my code.

Now here's the kind of thing I'm using in my code in order to play the correct animation based on an angle:
Image

I have the feeling that I am doing something wrong. Is there a more efficient way to play an animation based on an angle than the one showed above?

By the way the code above works for a moveto behavior. For a bullet behavior (shotAngle variable set to bullet.angleofmotion) I'll have to change every value above 180 by their negative counterparts or it won't work.

Bonus question : Is there some way to tell C2 that a certain variable is an angular value? i.e. convert every value above 180 to their negative counterpart or the opposite. That would already help me with the 270°/-90° problem.

Thanks !
B
32
S
6
G
1
Posts: 353
Reputation: 2,821

Post » Mon Jul 31, 2017 3:36 pm

Ordinarily I would just say name the animations by their angles, and just set it that way, but you are using mirrored, so maybe do a loop and compare the int(angle/n)*n to int((360/loopindex)/n)*n
Where the loop, and n are equal to the number of animations.
If you have 8 animations the angles would go in increments of 45 since 360/8= 45
So index 0= 0, index 1 = 45, 2 = 90, etc.
Image ImageImage
B
170
S
50
G
179
Posts: 8,381
Reputation: 113,431

Post » Tue Aug 01, 2017 1:58 pm

Thanks for the reply @newt , however I have a hard time understanding what you mean.

First, I guess you mean that n= 360/number of animations, right? Because if I follow your suggestion and state that n=number of animations, then angle/n will return a number between 0 and 45, which I really don't see what I'm supposed to do with. Same with int(angle/n)*n, which will simply return my angle as an integer.

Next, I'm still in trouble with the -90 AKA 270 angle problem.

I tried to make something based on your proposal, and here's what I came up with:
Image
I could use tokenat in conjunction with the loopindex in order to launch animation automatically and use less events, but it doesn't really apply as I use only 5 animation and mirror 3 of them, so I went for the simple version to avoid renaming all the animations.

Is this the kind of thing that you meant, or did I miss something?
B
32
S
6
G
1
Posts: 353
Reputation: 2,821

Post » Tue Aug 01, 2017 3:21 pm

How many animations do you have? It seems like 8.

360/8 = 45
Each animation is allowed 45 degrees.

If you have 4 animations, then change 45 to 90.
360/4= 90.


Try
Floor(Shot.Angle/45)
This will round down to give a whole number as a result.
0,1,2,3,4,5,6,7

Or Try
Ceiling (Shot.Angle/45)
This will round up to give a whole number as a result.
1,2,3,4,5,6,7,8

Now compare the result and set the animations.
0 will be right, 3 will be left..
Im not sure of up and down...


Set Variable :Floor(Shot.Angle/45)
Variable = 0: do this
Variable = 1: do that.

Variable should be an instance variable.


I personally wouldn't use a loop. It is setting the animation 8 times per tick instead of once.
The first 7 times don't count because all 7 are overwritten by the 8th.
Once per tick should be enough, unless I'm missing some info.
Last edited by Unconnected on Wed Aug 02, 2017 12:46 pm, edited 1 time in total.
B
67
S
37
G
13
Posts: 241
Reputation: 12,760

Post » Tue Aug 01, 2017 5:20 pm

I'll try to do an example.
Image ImageImage
B
170
S
50
G
179
Posts: 8,381
Reputation: 113,431

Post » Tue Aug 01, 2017 6:46 pm

https://www.dropbox.com/s/esom6cnhzv9c4 ... .capx?dl=0

I should point out that it doesn't use mirrored, as it really complicates things for a relatively small savings on frames.

Also source of zombies.
https://opengameart.org/content/zombie-sprites
Image ImageImage
B
170
S
50
G
179
Posts: 8,381
Reputation: 113,431

Post » Wed Aug 02, 2017 8:00 am

@newt
@Unconnected
Thanks a lot! Very clever way to achieve that in only one event!

For future reference, here is the code provided by newt. I just added 22.5 to the variable angmo in order to have the zombi move right if I click at a -0.1 angle from him (otherwise he's moving up-right, which is unnatural):
set angmo to int((angle(touch.X,touch.Y,Sprite.X,Sprite.Y)+22.5-180)/45)*45
Image
The second event is just to display the angle values.
B
32
S
6
G
1
Posts: 353
Reputation: 2,821

Post » Wed Aug 02, 2017 10:37 am

I think I find a way to implement the 3 mirrored animation as well. It saves 3/8 of the memory, weight, etc. so it isn't even an option to implement mirrored animations in the animation editor. I did the test just to be sure, and if you mirror a sprite into the animation editor, it will be a different sprite when exported.

Here's the code:
Image

Basically, I consider that top is 0°, right is 90°, left is -90°, etc. I named my 5 animations accordingly and play the animation based on this new angle.
If the variable has a negative value, the animation is simply mirrored.
Best of all, it automatically convert all 180+ angles to their negative counterparts, so no more confusion between MoveTo's 0-360° angles and Bullet's -180/180° angles.
B
32
S
6
G
1
Posts: 353
Reputation: 2,821

Post » Wed Aug 02, 2017 2:25 pm

Nice.
Glad you got it figured out.
We really do need have some sort of standard conversion for those negative angles.
I mean you would expect software that simplifies game making to make everything work together.
Image ImageImage
B
170
S
50
G
179
Posts: 8,381
Reputation: 113,431


Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 9 guests