Question about moving and bouncing a ball

dekisimkedekisimke Member Posts: 24
edited November 2014 in Working with GS (Mac)

In the game there is only 1 ball. The other 2 balls on the image illustrates different scenarios. This game has no gravity, no friction. the ball is supposed to keep moving and increase gradually in speed when it hits the box/square.

Problem A, bounce:
How do I get the ball to accurately change direction when it hits the (rotated by player) box/square.
I want it to go as the green and red arrows illustrates.
The ball must also bounce away from the edges/walls of the screen so it stays inside the scene.

Problem B, movement:
With what behavior should I move the ball around with?
I want the ball to move around the scene with a steady speed (not accelerating). Right now I'm using self.motion.linear velocity. Is this what I should be using?

image

Comments

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949

    We ask that you not use ALL CAPS or tons of exclamation marks in your thread titles. Also, please use descriptive titles such as the one I changed this to.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • SocksSocks London, UK.Member Posts: 12,822
    edited November 2014

    @dekisimke said:
    Problem A, bounce:
    How do I get the ball to accurately change direction when it hits the (rotated by player) box/square.

    Just the plain old physics engine should handle this just fine, the only thing you'd need to do is to switch 'friction' to zero (otherwise the angle of reflection doesn't quite match the angle of incidence).

    @dekisimke said:
    The ball must also bounce away from the edges/walls of the screen so it stays inside the scene.

    You need to place something in the way, a 'wall' actor will do the trick.

    @dekisimke said:
    Problem B, movement:
    With what behavior should I move the ball around with?
    I want the ball to move around the scene with a steady speed (not accelerating). Right now I'm using self.motion.linear velocity. Is this what I should be using?

    Yes, move the ball with 'linear velocity'.

  • dekisimkedekisimke Member Posts: 24
    edited November 2014

    @tatiang said:
    We ask that you not use ALL CAPS or tons of exclamation marks in your thread titles. Also, please use descriptive titles such as the one I changed this to.

    Okay boss!

    @Socks said:
    Just the plain old physics engine should handle this just fine, the only thing you'd need to do is to switch 'friction' to zero (otherwise the angle of reflection doesn't quite match the angle of incidence).

    Yes, move the ball with 'linear velocity'.

    So if I start the game by moving the ball with linear.velocity.y = 150... What behavior do i use on the box to make it move away in the right angle? "Bounce" wont change the direction of the ball since our linear.velocity.y is 150. it just keeps bouncing against the wall.

    @Socks said:
    You need to place something in the way, a 'wall' actor will do the trick.

    I have already put up walls around the screen edge (they're white colored, that's why you can't see them) and making the ball bounce was easy with change attribute: self.Motion.Linear Velocity.Y to
    -self.Motion.Linear Velocity.Y.
    Same thing on the horisontal walls but with x instead of y.

    The player can put the white box anywhere on the black area and gets to rotate the box before deciding in what angle to place it.

    The command i used on the vertical and horisontal walls "self.Motion.Linear Velocity.Y(or x)" won't work here since the box will always be rotated in different angles and if that command is used, the ball will just change the x or y direction. I am in a hold up with my game because of this and really want to move on but i am stuck :/

  • SocksSocks London, UK.Member Posts: 12,822
    edited November 2014

    @dekisimke said:
    So if I start the game by moving the ball with linear.velocity.y = 150... What behavior do i use on the box to make it move away in the right angle?

    Just use a Collide behaviour in the ball actor - and set it to collide with the box.

    @dekisimke said:
    "Bounce" wont change the direction of the ball since our linear.velocity.y is 150. it just keeps bouncing against the wall.

    I don't know what any of that means.

    @dekisimke said:
    . . . making the ball bounce was easy with change attribute: self.Motion.Linear Velocity.Y to -self.Motion.Linear Velocity.Y. Same thing on the horisontal walls but with x instead of y.

    :o :)

    @dekisimke said:
    The command i used on the vertical and horisontal walls "self.Motion.Linear Velocity.Y(or x)" won't work here since the box will always be rotated in different angles and if that command is used, the ball will just change the x or y direction. I am in a hold up with my game because of this and really want to move on but i am stuck :/

    I would just use the built in physics engine instead of trying to program a new one yourself, you can do it, but it takes a lot of time and effort to get it working well.

    Let me make you a demo . . . . back in a minute . .

  • GSGURUGSGURU AtlantaMember Posts: 48

    PPL PLZ COMMENT ON MY DISCUSSION

  • SocksSocks London, UK.Member Posts: 12,822
    edited November 2014

    @dekisimke said:

    Here you, this should give the basic idea, the only thing to note is that I have turned the 'friction' value for the ball to '0' (you do this is the ball's attribute panel) - leaving it as the default value of 3 means it will eventually slow down to a stop as each collision takes off a little bit of its velocity.

    Demo project: https://www.mediafire.com/?qx2x31nyzqn14y1

  • dekisimkedekisimke Member Posts: 24
    edited November 2014

    @Socks said:
    Here you, this should give the basic idea, the only thing to note is that I have turned the 'friction' value for the ball to '0' (you do this is the ball's attribute panel) - leaving it as the default value of 3 means it will eventually slow down to a stop as each collision takes off a little bit of its velocity.

    Demo project: https://www.mediafire.com/?qx2x31nyzqn14y1

    I can't open your file with gamesalad, I'm on a mac your file might be from a pc or something like that...
    Anyhow, I might not have been clear about the physics in my game but I have fixed the motion issue that was question b.
    The ball is now moved by the "move" behavior: Direction = self.rotation. Speed = BallSpeed.Game Attribute. There is no gravity or friction issue in the game.

    Let's say the balls angle/rotation is 90* at start of game. That makes it move up with the speed assigned from the game attribute "BallSpeed" which is 150.
    Then it hits the top wall and is supposed to rotate to 270* and move down.
    But let's say my balls angle is 45* and hits the top wall, that is supposed to make my ball rotate to 315*.
    If the balls angle is 135* and hits the top wall, that is supposed to make my ball rotate to 225*.
    We can be more precise, let's say the balls angle is 32*, new angle for the ball would be 302*.

    That's only the bounce mechanics for the top wall, now deciding the new angle for the ball when it hits the box is where it gets tricky for me. I am not so good with maths and don't know how you are supposed to type it in to the expression editor.

    TL;DR: I need the math for "X":
    Ball > Rule: Collide with wall or box > Change Attribute > self.rotation to X

  • SocksSocks London, UK.Member Posts: 12,822

    @dekisimke said:
    I can't open your file with gamesalad, I'm on a mac your file might be from a pc or something like that...

    I am on a Mac, using 12.8 (RC), try adding .gameproj to the end of the file name.

    @dekisimke said:
    We can be more precise, let's say the balls angle is 32*, new angle for the ball would be 302*.

    I think it should be 328 ?

    @dekisimke said:
    TL;DR: I need the math for "X": Ball > Rule: Collide with wall or box > Change Attribute > self.rotation to X

    Like I say above, I would just use the built-in physics engine and collide behaviour, it seems pointless trying to re-programme something that GameSalad already does for you.

  • dekisimkedekisimke Member Posts: 24

    @Socks said:
    I am on a Mac, using 12.8 (RC), try adding .gameproj to the end of the file name.

    Tried doing that but got this message when i try to open: "The document “bounce bounce.gameproj” could not be opened. Unable to read file." :(

    @Socks said:
    I think it should be 328 ?

    Yes you are correct, as i said, I'm not the best with maths hehe

    @Socks said:
    Like I say above, I would just use the built-in physics engine and collide behaviour, it seems pointless trying to re-programme something that GameSalad already does for you.

    Could you please explain to me what the built-in physics engine is? Where can I find it?
    The collide behavior is not making the ball move the other way as intended, instead it keeps moving in the same direction, pushing against the wall when it reaches it. What I need is to make the ball turn the other way and keep moving until it collides with a wall/box. Then the angle of the ball + the angle of the wall/box is going to define the balls new angle it will move in.
    I really wish i could open your file you sent me so I could see what it looks like! Also thanks for trying to help Socks, very appriciated!!

  • SocksSocks London, UK.Member Posts: 12,822
    edited November 2014

    @dekisimke said:
    Tried doing that but got this message when i try to open: "The document “bounce bounce.gameproj” could not be opened. Unable to read file.

    Hmmmm . . . not sure what that is ?

    Here's a quick video instead:

    @dekisimke said:
    Could you please explain to me what the built-in physics engine is? Where can I find it?

    It is basically using the behaviours provided for you in GameSalad, it consists of little more than using a behaviour to give actor some velocity and then using the collide behaviour so the actors collide with each other.

    You can also play with the physics attributes for each actor, changing things like friction and bounciness changes the way the actors behave.

  • dekisimkedekisimke Member Posts: 24

    @Socks said:
    Here's a quick video instead:

    THANK YOU! You solved my problem, but i have one question. The ball starts moving up in 90* with the speed 150. I want this speed to increase every time by 20 when the ball hits the box. How do I do this?

    I have an attribute called BallSpeed that is set to 150. and for every time the ball collides with the box, BallSpeed is set to BallSpeed+20. But this does nothing on the Change Velocity behavior... If I copy the Change Velocity into the rule "Ball collide with Box" the speed issue will be fixed but the direction of the ball is now messed up. Is there a way to just update the speed in Change Velocity behavior?

  • SocksSocks London, UK.Member Posts: 12,822
    edited November 2014

    @dekisimke said:
    I have an attribute called BallSpeed that is set to 150. and for every time the ball collides with the box, BallSpeed is set to BallSpeed+20. But this does nothing on the Change Velocity behavior... If I copy the Change Velocity into the rule "Ball collide with Box" the speed issue will be fixed but the direction of the ball is now messed up. Is there a way to just update the speed in Change Velocity behaviour?

    Try increasing the bounciness of the ball.

  • dekisimkedekisimke Member Posts: 24
    edited November 2014

    @Socks said:
    Try increasing the bounciness of the ball.

    Did not work as intended, what happens is that the speed increases every time the ball hits the walls/box. I want the ball to increase exactly by 20 in speed only when it hits the box.

    So speed level 1 = 150, speed level 2 = 170, speed level 3 = 190 etc.

    Edit: Nvm, i fixed it by increasing the box's bounce to 1,1

  • dekisimkedekisimke Member Posts: 24

    All in all, my issues are solved. I can now continue on my game! Thanks Socks!

Sign In or Register to comment.