# bouncing objects

Discussion in 'Programming' started by pgvago, May 14, 2019.

1. ### pgvagoMember

Joined:
May 14, 2019
Posts:
9
hello from italy (again);

can i anybody suggest an idea on how you would make an object bounce off a wall.
the antifairy in zelda is ideally what i am trying to achieve.

i would love a suggestion on how you would tackle the problem.

thanks, pietro.

2. ### GzebraMember

Joined:
Aug 3, 2017
Posts:
63
use speed and direction for movement, and randomly add or subtract 90 to direction when the object collide with the wall.

3. ### TimothyMember

Joined:
Aug 7, 2016
Posts:
64
Except it's not random.

Simple solution.
If colliding on x axis, reverse hspeed.
If colliding on y axis, reverse vspeed.

4. ### pgvagoMember

Joined:
May 14, 2019
Posts:
9
let me do some experiments on your suggestions and i'll get back to you.

pietro.

5. ### pgvagoMember

Joined:
May 14, 2019
Posts:
9
how can i check if i am colliding on the x or y axis?

thanks, pietro.

6. ### BattleRifle BR55Member

Joined:
Jun 24, 2016
Posts:
947
Two collision checks. One for the x axis plus hspeed, one for the y axis plus vspeed. Whichever returns the hit, reverse the appropriate variable.

Joined:
May 14, 2019
Posts:
9

pietro.

8. ### RattlejawMember

Joined:
Jun 24, 2017
Posts:
18
You can try this. It's a very simple method.

In the object that is bouncing off walls.

CREATE EVENT
direction = (random_range(45,135));
speed = 2;

Add a collision event with your obj_wall. In that event, use the following function.

move_bounce_all(true);

The object should move around and bounce off of walls. You can change the values in the direction and speed to tweak it. If you don't want it's starting direction to be so random, you can just have direction = 45 or something like that.

Where ever you place or spawn the object in the room, it does effect it's moving pattern.

Last edited: May 18, 2019
9. ### pgvagoMember

Joined:
May 14, 2019
Posts:
9
unfortunately there isn't much literature on move_bounce_all ().

does the object bounce bounce at the same angle?

thanks, pietro.

10. ### JaanMember

Joined:
Dec 6, 2018
Posts:
1
Gidday.

Found this thread and it's along the lines of the problem I'm having. So... I've been through the Breakthrough tutorial and am trying to impliment multiball. A single ball works and bounces perfectly.

If I pick up the multiball powerup two extra balls are created, move upwards at the correct speed... but all balls lose the ability to bounce. All three balls will move upwards through the bricks they're meant to bounce off. Neither will they bounce off the boundaries. I'm certain the problem is with move_bounce_all() but I just can't figure it out.

• If I comment the move_bounce_all() line then the single ball won't bounce, as expected. It moves through the bricks and gets stuck on the top boundary, same as the multiball problem.
• If I place move_bounce_all() in the Step event the balls collide with the brick and get stuck, looking like they're very quickly vibrating.

o_powerup object code when collecting a multiball powerup:
Code:
```case 9:  //Triple ball
//Set multi ball control on
o_multiball_control.multi = true;

//Create new balls
for (var _i = 0; _i < 2; _i++){
instance_create_layer(o_ball.x, o_ball.y, "Ball", o_ball);
}
instance_destroy();
break;
```
o_ball object code when colliding with a brick:
Code:
```if bounce_enable{
bounce();
}

with other{
//Determin powerup drop
var _chance = random(10);
if _chance < other.pu_chance instance_create_layer(x, y, "Powerups", o_powerup);
//Float point value
spawn_floating_text(value);
instance_destroy();
}

global.player_score += o_Brick.value;
```
bounce() script code:
Code:
```//Get direction ball came from
var _dir = direction - 180;

//If ball is overlapping the brick, bounce will be incorrect
//Move the ball back until the bounding boxes are not overlapping
while place_meeting(x, y, other){
x += lengthdir_x(1, _dir);
y += lengthdir_y(1, _dir);
}

//Enable bouncing
move_bounce_all(true);

//Increase speed
if speed < max_spd speed += spd_increase;
```
o_ball object code that is used to shoot the ball the first time, or auto set speed and direction if multiball is active:
Code:
```//Control the ball when new ball
if !o_multiball_control.multi{
//Keep ball on the bat
if on_bat{
x = o_player.x;
y = o_player.bbox_top - sprite_height /2;
}

//Shoot ball
if o_player.shoot && can_shoot{
speed = initial_spd;
direction = dir;
on_bat = false;
can_shoot = false;
}
}

if o_multiball_control.multi{
speed = initial_spd;
direction = dir;
move_bounce_all(true);
}
```
The move_bounce_all(true) in the o_ball code above is what causes the balls to collide with the bricks and stick.

Any help and a good explanation of how move_bounce_all() works will be much appreciated. The explanation of this function is confusing.

Cheers!

11. ### RukiriMember

Joined:
Sep 25, 2016
Posts:
140
This is what I use:
Create:
Code:
```degree = -1;
spd = 2;
knockback = false;
```
Step:
Code:
```if (knockback == true) {
}
```
Alarm:
Code:
```knockback = false;
```
Collision (many ways to check):
Code:
```//degree = radtodeg(arctan2(wall, player, etc.y - enemy.y, enemy.x - wall, player, etc.x));
degree = arctan2(wall, player, etc.y - enemy.y, enemy.x - wall, player, etc.x) * (180 / pi);
radians = pi * degree / 180;
knockback = true;
alarm = 20;
```
Basically you want to either get the X/Y of a Wall Tile, The Player Object, etc, and then this will push the object in a direction.

So you only want to push it for 1-2 seconds use a timer variable, set delta time and then timer += 1 * delta and then you can set if timer>= 2 { knockback = false; } and you're good to go.

If you are using tile collisions you setup wall collisions for knockback and normal collision maps "so the enemy can't move past". If using sprite/object collisions use if place_meeting(x, y, wall) { // math for bounce back here } You can add an offset of 2 or 3 pixels and then check the players direction to adjust the offset but you don't need to write the bounceback 4 times just the offset part so like.

Code:
```with (player) {
if place_meeting(x-1, y, player) || place_meeting(x+1, y, player) || place_meeting(x, y-1, player) || place_meeting(x, y+1, player) {
// bounceback code
}
}
```
with walls if place_meeting(x, y, wall) { // bounce code } is fine.

Also so the enemy doesn't bounce off the walls while just moving around make sure knockback is true so that it only happens IF it was originally knocked back by the player.

I do not use built invariables, I like making my code reusable say if I were to go to another game engine, than it's simply just copy and place

Last edited: Jun 11, 2019