Reducing lines of code for sprite angle checks

0 favourites
  • 7 posts
From the Asset Store
Compete online with friends and family with most popular Dots n Lines (Dots and Boxes) game
  • Hey,

    I've managed to create a moveable pseudo 3D sprite, but I wanted to add a total of 360 frames, and set the frame for each degree.

    Example of what I have now (it's currently got 36 frames and lines of code)

    The car sprite images is rendered in a 3D program, it 'rotates' with each image counterclockwise. So just so you know, 0 degrees started with (out of the total of 36 images) image 19. That would be frame 18 - as you can see in the code in C3 considering it's zero based.

    Anyway, I was wondering if it's possible to avoid having to make 360 lines of code? It already felt like a huge chore to do those 36 lines, copying and changing every bit!

    Perhaps it's possible to reduce it to a handful?

  • You can do it in a loop. For example, if you need to check in 10 degrees intervals:

    Repeat 36 times
     Car.MoveTo.MovingAngle within 5 degrees of (loopindex*10)
     	Car set frame to loopindex
    	Stop loop
    

    I don't quite understand how the animation is made in your case, so you might have to set the frame to something like (36-loopindex).

  • OK, thanks, the code on line 2 you posted, which condition are you using?

    And what do you mean with (36-loopindex)? Do I need to add that literally to the action as:

    Set animation frame to: (36-loopindex)

    Or did you mean something else.

    Oh and, is this code for 36 frames or 360? I'm trying to understand why you use 5 degrees, and use multiply loopindex by 10.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • OK, thanks, the code on line 2 you posted, which condition are you using?

    "System Is Within Angle", it's a similar condition to "Is between angles"

    And what do you mean with (36-loopindex)? Do I need to add that literally to the action as: Set animation frame to: (36-loopindex)

    Yes, but I don't know if this will be a correct expression in your case.

    Oh and, is this code for 36 frames or 360? I'm trying to understand why you use 5 degrees, and use multiply loopindex by 10.

    It's for 36 frames. I loop in 10 degrees intervals from 0 to 350 degrees - 0, 10, 20, 30 etc. And for each interval checking if car angle is within 5 degrees of it. For example, for loopindex=8 it will check if car angle is between 75 and 85 degrees.

    .

    If your animation has 360 frames, then you can simply set frame to moving angle value. But you will need to convert it to positive number:

    Set frame to (Car.MoveTo.MovingAngle+360)%360

  • If you have 36 frames for each 10 degree increment, you should be able to just set the animation frame to a single formula something like ((Car.MoveTo.MovingAngle+360)%360)/10. However that will map 0-10 degrees to the first frame - if you want the first frame to be -5 to +5 degrees, then just shift it back by 5 degrees with something like ((Car.MoveTo.MovingAngle+355)%360)/10

  • ((Car.MoveTo.MovingAngle+355)%360)/10

    I'm not sure this formula is correct. It will return frame 0 if the angle range is between -5 and 5. However, when the moving angle is between 5 and 15, the frame needs to be 1.

    I think this should do the trick:

    Car set frame to round(((Car.MoveTo.MovingAngle+360)%360)/10)%36

  • > ((Car.MoveTo.MovingAngle+355)%360)/10

    I'm not sure this formula is correct. It will return frame 0 if the angle range is between -5 and 5. However, when the moving angle is between 5 and 15, the frame needs to be 1.

    I think this should do the trick:

    Car set frame to round(((Car.MoveTo.MovingAngle+360)%360)/10)%36

    Thanks guys!! You're amazing. I knew it was possible, I just do not possess the skills to put that all in one line of code.

    dop2000 Your code works perfectly. Aside that, I just needed to make sure the starting position of the rendered animation was set to the right (0 degrees), and that it would rotate clockwise since that's how angles work in C3.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)