How do I quickly and easy figure out which side of a sprite.

0 favourites
From the Asset Store
Build your own spaceship for your side scroller space shooter games with various components.
  • Hello.

    On collision with a sprite, how do I figure out which side of the sprite another sprite (ball) is colliding with?

    For example if the ball is hitting the bottom edge of a sprite. Or its left..

    Should work with sprites that are generated dynamically and not slow down with hundreds of sprites on layout.

    I am open to plugins if it shorten down the coding.

    Something like rexrainbow 's Boundary but externally and not clamping/wrapping.

  • You could use

    "Is Overlapping at Offset" (-25, 0) for left side

    "Is Overlapping at Offset" (25, 0) for right side

    and make it a dual condition by adding System> Trigger Once while True to each

  • With big offset it looks like the ball is bouncing mid air. So I l lowered to 2 px offsets only. It works in slow but when on low spec or high speed it would be less exact. hmm.

  • You should explain what you are trying to make here. Set up the scene for us and we can probably offer a simpler solution.

    Also, include a capx or screenshots.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I wanted to make a more reliable breakout style game or just ball/ricochet that would bounce off in proper angles and not fly off randomly. (all in the style of 70-early 80s era game) Also not escaping the play field.

    For the not escaping play field I used rexrainbow plugin Boundary which works nicely.

    For the better, more predictable moving of the ball/bullets I used custom movements instead for bullet. It is so much better!! (the "Reverse custom movement" for example to switch the angle)

    The goal is indeed to have a good code even for high speed or low spec computers.

    Here's a capx just be aware you have to have Boundary plugin.

    Use shift to add ball, you can add more balls. The paddle's top will change the ball's angle.

    Left/right arrow to move the paddle.

    Spacebar to pause game, enter to unpause.

    Try add 10+ balls in this game and study how the balls starts to go through the blocks.

    (wait with the first ball until you got the screen filled with at least 100+ blocks)

  • I think you are over-thinking this project. I made a breakout game a few years ago and I did not need any plugins or custom movements. Unfortunately, I can't offer much more help then that. I don't want to install plugins.

  • Tekniko

    it is ok, you do not have to help me.

    Which movement did you use, if you remember? Bullet?

    Many projects with breakout style games made with C2, including the ones in tutorials and examples (and my own older projects on break out style games), have this problem with bouncing off in unpredicted way once in a while if they use bullet behavior plus collision with solids - Bounce off Object. (for example going into 45 degrees and then suddenly bounce off in like 23 degrees and not -45)

    Boundary plugin is a nice way to keep the ball within the play area without writing too much events.

    But I am open to debate of course, I want to perfect the ricochet process.

    This is just experiment with goal to see if it can be polished into max.

  • I can't open the capx now, but since you say breakout I'm guessing you'll only have unrotated rectangles that the ball bounces against.

    One way to do it is to move the ball backwards when it collides with a block, which can be done with the push out of solid action of custom movement. Then you can use dom's idea to find what direction walls are.

    Bouncing off horizontal and vertical walls is just a matter of reversing the y or x velocity of the object. You can also calculate the bounce angle if you know the angle of the wall hit but I don't recall the formula off hand.

  • check out this tutorial, It might be helpful

    Subscribe to Construct videos now

    should be about 3:00 min or a little more in when he sets up how it collides with the ball and paddle

  • ZeroBelow I have that project, it was one of the first tutorials I followed. <img src="{SMILIES_PATH}/icon_e_wink.gif" alt=";)" title="Wink">

    R0J0hound I tried as you suggested, push out of solid action, and I thought it would be a good solution. But the ball will just pass through if I choose "opposite direction". When choosing "near" it will go "around" the block in funny way, so at least that one is working.

    If you could figure out the formula I would be very happy.

    To everyone, here I have made another project, but not in CAPX because of your not wanting to download plugin. Tekniko

    http://www.funnybunny.se/files/ballexamples/

    Four examples of different types.

    Look at how the classic bullet would go off in strange angles when sped up or after a while.

    and the classic config of solid walls and bullet would escape at max speed (on my computer at least) but this problem won't happen with the Boundary plugin.

    I hope you will understand what I mean with keeping proper angles AND minimizing risk of escapey ball.

    I know thicker walls = better chance to keep in the ball.

  • Try this formula and see if it works..........(2*(angle(wall.x,wall.y,ball.x,ball.y)-ball.angle)+ball.angle)+180

  • granpa I used your formula like this: [attachment=0:324vpiwa][/attachment:324vpiwa]

    And the ball bounces off in strange angles, not what I would like.

    I am using this for the blocks and not the walls as I use plugin Boundary for the walls and there I could determine the direction thanks to the top, bottom, left, right side.

  • I edited the formula to replace the ball.angle to ball.custommovement.movingangle but still irregular angles. The angle result is quite high too, 389 degrees, 455 degrees, etc

    I edited the formula and until I removed the 2* in beginning and the +180 at the end the ball started to bounce and not pass through blocks, and the angle result is in more "realistic" range, but still not perfect angles.

  • The formula I gave you was a derivative of a standard bounce formula when you know the angle......sorry it did not work . The original formula is 2*(out.angle-in.angle)+in.angle...... where out angle is what you want to bounce off and in angle is the ball .

  • granpa thanks, I edited my post, read the last paragraph... Or better yet:

    2*((angle(Sprite.x,Sprite.y,ball.x,ball.y)-ball.CustomMovement.MovingAngle)+ball.CustomMovement.MovingAngle)

    This is "closing" but not perfect, bouncing and not passing through blocks, but in wrong angle.

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