Bouncing grenade in top down view using scaling

0 favourites
  • 11 posts
From the Asset Store
Bouncing ball
$4 USD
60% off
Template for a bouncing ball game, fully documented in comments and video
  • In my top down game, I'm trying to create a realistic grenade which when thrown by the player does the following:

    1) Gives the appearance of an arc by increasing the size/scale of the grenade sprite then decrease back to normal size to give the appearance of height (rather than using shadows)

    2) Allow player to charge the grenade by holding down the mouse button, the greater the charge, the further the distance thrown and the greater the "fake" height

    3) Bounce off any solids (regardless of the fake height)

    4) Rotate the grenade sprite a random number of degrees while it is in the air on its origin imagepoint.

    5) Bounce the grenade off the "floor" 3 times before coming to a stop (i.e. increase and decrease scale 3 times, in smaller increments each time).

    6) Have grenade explode several seconds after being thrown and generate a short lived blast radius

    In the capx below I have items 2, 3 and 6 implemented but not sure how to go about items 1, 4 and 5.

    Any suggestions appreciated.

    CAPX: sendspace.com/file/pzx3q5

  • Take a look at my game: http://balloonwarsgame.com. Playing the first level will give you some idea of how it works, specifically the water balloons. If you see anything you want to happen similar to the balloons for your grenades, let me know. I am actually working on changing the balloons arc to be shorter the closer you throw it, and bigger for the further away you throw it. Whatever you might want to know, I will tell you what I did to make it happen.

  • Concept wise it's similar to what I'm going for but your arc is from a first person perspective (large to small) whereas I'm looking for a straight top down arc (small - large - small). I'd be interested to know how you scale the balloons after throwing.

    The other things I'm trying to do for regular grenades that I guess don't apply to water balloons are multiple bounces and rotation.

  • For the following to make sense, I created two global variables called GameWidth and GameHeight in my project.

    At the start of a layout I set GameHeight = ViewportBottom(0) - ViewportTop(0), and GameWidth = ViewportRight(0) - ViewportLeft(0).

    The following is assuming that the grenade sprite width is the same as its height

    Give the grenade variables called MaxSize, MinSize, Peaked, and BouncesRemaining. (You can always change these to names that make more sense to you. All of these are Number, except for Peaked which is a Boolean)

    When you spawn a grenade:

    Set Grenade.MaxSize = GameHeight * 0.4 (play around with this value to get it the size you want)

    Set Grenade.MinSize = GameHeight * 0.1 (again, whatever value works for you)

    Now, I'm guessing that the size the grenade will start at will be a little bigger than when it hits the ground.

    So, set Grenade.Height = Grenade.MinSize * 1.2

    Set Grenade.Width = Grenade.Height

    Make an event:

    For Each Grenade                                      | Rotate Grenade Clockwise (or counter clockwise) 500 * dt

    If Grenade.RemainingBounces > 0             (you can try other numbers instead of 500)

                  

           | (subevent)

           |   If Peaked = False | Set Grenade.Height = Grenade.Height + ((Grenade.MaxSize - Grenade.Height) * dt * 300)

                                 (300 might be too little or too big, so try different values)

                                     | Set Grenade.Width = Grenade.Height

                      |   (subevent)

                      |   If Grenade.Height > Grenade.MaxSize * 0.99 | Set Peaked = True (if you

                                                             find that the grenade spends too much

                                                             time at its peak, try another number

                                                             like 0.97 or 0.95 instead)

           | (subevent)

           |   Else                 | Set Grenade.Height = Grenade.Height - ((Grenade.MaxSize - Grenade.Height) * dt * 300)

                                    | Set Grenade.Width = Grenade.Height

         

                      |   (subevent)

                      |   If Grenade.Height < Grenade.MinSize * 0.01 | Subtract 1 from Grenade.RemainingBounces

                                                                                                            | Set Grenade.Peaked to False

                                                                                                            | Set Grenade.MaxSize to Grenade.MaxSize * 0.5

                                                                                                             (play around with this number, such as 0.4, 0.3, etc.)

                                 |   (subevent)

                                 |   If Grenade.RemainingBounces < 1 | (The grenade is done bouncing, so you could either

                                                                                                         make it explode here, or it just stops)

                                       

    Let me know how this works out for you

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I probably would have cheated and put an invisible side view grenade just off screen, bouncing on invisible ground. Then used it's Y distance from the invisible ground to set the scale of the actual grenade, the X along the invisible ground to determine the distance traveled.

    edit: (in other words, the x would adjust the speed to match the scaling from the Y, so it would be a literal translation of the motion from the top view.)

  • Yes, that's a great idea! I think that would be pretty easy, although if there were a lot of grenades, I don't know if the physics would slow down the game.

  • I created one for an example a while back using the EaseTween behavior.

    Not sure if it what's you are after, but you could then adjust the EaseTween settings depending on power. Like adjusting the Duration of the tween.

    Press Z to throw, that's all it does :).

    http://www.elf-games.com/games/grenade/

    Link to capx, requires EaseTween behavior.

    https://www.dropbox.com/s/vzucs5y7uon28vr/grenade-throw.capx

  • Can't you use the behaviour/movement/sine to adjust the size of the grenade sprite over the length of the throw ? It should give you the correct arc and could save some maths ?

    Edit

    after a quick play

    grenade test

    capx

    very rough but maybe ...?

  • You may be able to do my solution with bullet behavior. Which is much faster than physics. It has gravity and bounce calculation too.

  • sgtwombatstudios: your logic makes sense but am having trouble getting it working. See CAPX: sendspace.com/file/0ayn32

    RamPackWobble: your method gives a realistic arc, but I'd like to avoid creating separate sprites for the bounced grenade. Also I'd like to be able to modify the number of bounces without having extra code for each bounce, maybe using a function?

    Paradox, an example capx would be helpful to better understand what you're suggesting.

    ArcadEd: Which version of EaseTween are you using? Couldn't get your file open.

  • Not sure, it was a while ago when I downloaded it. Before LiteTween was available.

    I haven't updated it.

    I can try too and resave the capx

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