How do I Bounce without behaviors ?

0 favourites
  • 11 posts
From the Asset Store
Bouncing ball
$9.99 USD
Template for a bouncing ball game, fully documented in comments and video
  • Hi

    I want bounce a ball on differents rectangular boxs without behaviors because i need to keep the x/y variables and then pass them through a relocation pass before the ball is drawn to the screen...

    So i move the ball with :

    System every tick - ball set X to Self.X+Self.speed_x*dt
                                    - ball set Y to Self.Y+Self.speed_y*dt
    [/code:mnqr0lnu]
    
    I have the angle of motion of the ball with a function that i create.
    
    For the collision with a box i use :
    
    [code:mnqr0lnu]
    Ball is overlapping box 
    [/code:mnqr0lnu]
    
    Now i don t know how to use the angle of motion to bounce off the ball .
    I try to reverse x y coordinate with 
    
    [code:mnqr0lnu]
    X=cos(angleofmotion)
    Y=sin(angleofmotion)
    [/code:mnqr0lnu]
    
    But the result is not very correct , the ball bounce off but not very realistic..
    
    Thx for help
  • I don't have time to work on anything right now but quick thoughts are:

    1) Use the speed x and speed y variables to change your angle of motion. It's moving using those variables so altering it's X and Y coordinates is going to look strange since every tick you're still using those variables to push the ball in a particular direction. You can create another set of variables (e.g. X and Y constants - perhaps 0 and 200, respectively) to compare to whatever you change your variables to. If your speed variables are greater or smaller you bring each variable back to your constants every X seconds or tick with a small sum.

    2) Create imagepoints around your ball to determine where it's overlapping to determine the angle of motion you want and create a system that alters your X and Y speed variables accordingly.

  • I have a different idea for you.

    Create an invisible "shadow" clone of your ball sprite with Bullet behavior and "Bounce off solids=yes" and gravity if needed.

    Use its coordinates to adjust your main ball coordinates.

    You can even do this with physics behavior, see my comment in this post:

  • To be clear with my problem i post the file of my collision engine and i repeat that i can t use any behaviors of construct 2.

    worm1work.alwaysdata.net/test/test_collision.rar

    And what i try to do for the bounce off it's to convert my angleofmotion to have thoses results :

    0 degrees = (1,0)

    90 degrees = (0,1)

    180 degrees = (-1,0)

    270 degrees = (0,-1)

    And after i can invert direction of the bounce change x y to 1 or -1

  • Sorry, I didn't understand your explanation <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile">

    What I meant is this:

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

    Use another sprite with Bullet behavior just as a source of coordinates (X, Y, angle of motion, angle of reflection).

    You can then use these values in your formulas and reposition your main ball sprite - invert angles etc.

  • Isn't a bounce basically a negative value of x speed and y speed ( - drag/friction if necessary ), so if something is moving at yspeed of 10 and hits the bottom, y speed becomes 10*-1 ( or *-0.9 to simulate slowdown due to friction ) x speed stays the same. Hitting sides you multiply xpeed by -1 and y speed remains the same.

    At least this is the basic way to do it, no angles are necessary.

  • Thx BadMario for your explanation, it's a basic rebound that I wish so if I do not need the angle and I change the x and y depending on the side of the box touched, my question is how we determine the side hit ? thank you !

    I find this formule from an old game in AS3 ( flash game )

     // hold our ball position
        var bx:Number = _main.mcBall.x;
        var by:Number = _main.mcBall.y;
        var br:Number = _main.mcBall.width * 0.5;
    
        // get our bounds (assuming that the brick has it's registration point
        // in the center) - extend by the radius of the ball to cover the points
        var left:Number     = this.x - this.width * 0.5 - br; 
        var right:Number    = this.x + this.width * 0.5 + br;
        var top:Number      = this.y - this.height * 0.5 - br;
        var bottom:Number   = this.y + this.height * 0.5 + br;
    
        // check the position of the ball in relation to our brick
        if( ( bx >= left && bx <= right && by >= bottom ) ||    // bottom
            ( bx >= left && bx <= right && by <= top ) )        // top
        {
            _main.mcBall.ballYSpeed *= -1;
        }
        else // hit the left or the right
        {
            _main.mcBall.ballXSpeed *= -1;
        }
    
    [/code:2j92k98u]
  • well. itsort of is in that code.

    set each wall either as a variable ( leftwall = 0 for example ) or if using the actual object it would be leftwall = leftwallobject.X+leftwallobject.Width/2

    if ball.X < than leftwall ball.X= leftwall+ball.Width/2 and also multiply x speed by -1 to reverse it.

    Do same for each side, obviously for the right side it will be if ball.X > than rightside

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Here, this works fine. It should be pretty easy to adjust or alter depending on other things you'd like to do.

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

  • ultrafop I can t open your cap, version 247 and i have version 244 me

    Yes BadMario, i managed it like that and it works very well !

    Now I want to go further and I noticed that if I change the angle of my boxs the rebound is no longer realistic.

    I read that to manage this you have to go through vectors to calculate the angle of reflection. So it's not easy at all anyway ^^

    After several searches I found on the forum this plug in :

    I think it s the best solution to handle the collision on any object with a random angle but i dont know how to use it with a moving object ...

  • worm1

    To open a capx saved in newer version, rename it to zip, unpack to a folder, open .caproj file in Notepad and edit this line:

    <saved-with-version>24700</saved-with-version>

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