Simulate Bouncing

Member, BASIC Posts: 18

I have learned from a different discussion that you cannot get a very slow actor to bounce due to a Box2d rule, but is there any way to simulate bouncing using a rule? Maybe something using sin? Thanks!

• Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,948

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

• Member, BASIC Posts: 18

This would work for up and down bouncing, but my actor is on a flat plane where there is no gravity and it can move any direction and bounce off any wall. Also, this actor just bounces around and cannot be controlled by the player. Most importantly, the velocity should stay the same the whole time, like in a pong game.

• Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,948
edited November 2018

I'm no expert on GameSalad physics but I have coded a game where the actor reflects off of a wall to simulate a bounce, as you describe. I can't recall exactly how I did it (it was years ago) but I definitely used trig functions to calculate the reflection path based on the vector (direction) of the actor.

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

• Member, Sous Chef, PRO Posts: 2,873

If the actor collides with a vertical wall, multiply its xVelocity by -1.

If the actor collides with a horizontal wall (a floor or ceiling), multiply its yVelocity by -1.

• Member, BASIC Posts: 18

Does this mean I have to make a seperate actor for walls and ceiling? I am willing do that if that is the only way.

• Member, Sous Chef, PRO Posts: 2,873

Yes, thats probably the most straight forward way to do it.

I suppose that if you know the boundaries, you can skip checking for collisions with actors. Instead you could just test for the actor's x,y position.

When (self.position.x < 0) or (self.position.x > 1024) then multiply xVelocity by -1.

When (self.position.y < 0) or (self.position.y > 768) then multiply yVelocity by -1.

• Member, BASIC Posts: 18

Thanks! The walls are not near the boundaries of the screen though so I will have to use your previous idea.

• Member, Sous Chef, PRO Posts: 2,873

You are welcome! Glad it will help.

By the way, you can still use the x,y position method even if the "bounce" boundaries are nowhere near the screen edge.

• Member, BASIC Posts: 18

Lots of levels are like a maze, there are walls everywhere so using the x,y position method would take forever, but implementing a vertical wall and a horizontal wall was not as hard as I thought.

• Member, Sous Chef, PRO Posts: 2,873

Great! Glad its working like you want.

• Member, BASIC Posts: 18

However, there is one problem: corners. When the object hits a sharp corner, only one of the directional velocities is changed, and the bounce is not natural. Is there any way to fix this?

• Member, Sous Chef, PRO Posts: 2,873

Hmmm.... not sure what might be happening. Its almost impossible for the actor to hit both edges at the exact same instant -- so its unlikely to be that.

What does "the bounce is not natural" mean? Can you give more detail?

• Member, BASIC Posts: 18

I mean a corner that is facing inward compared to the actor, such as the actor hitting this L coming from below and to the left of the L. The actor will always go up left after hitting the corner, even if it hits the right side of the corner. If you still do not understand what i mean, I can send you a screenshot.

• Member, Sous Chef, PRO Posts: 2,873
edited March 2019

I can guess that something like that might happen because of the vertical and horizontal walls overlapping in different ways.

For example, in your outside "L", the the vertical "wall" might be underneath the horizontal "wall" so that their ends overlap each other. If both walls are the same color, it might look like the vertical "wall" goes all the way down to the bottom of the "L" but the actor is actually bouncing off the horizontal "wall" that is on top of it.

Try rearranging which walls overlap at the outside corners and see it that makes a difference in the way the actor bounces. You might also want to color the horizontal walls differently than the vertical walls so you can see what is overlapping what.

Then hopefully with some experimentation, you will be able to see why "the bounce is not natural".

• Member, BASIC Posts: 18

Thank you! This is definitely the issue, and although I can not fix it in this instance do to visual problems, it is good to know what the problem is.

• Member, Sous Chef, PRO Posts: 2,873

You are welcome!

I suppose you can try a quick fix by moving the bottom horizontal piece of the "L" one pixel down and one pixel to the right of its current location. That will make things bounce a little more 'normal'.

• Member, BASIC Posts: 18
edited June 2019

[deleted]

• Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,948

@stan_mcginnis You can only edit forum posts up to 24 hours after you post them.

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