November 30, 2020, 01:30:22 AM

### Author Topic: [bb] generic bouncing code by DarkEagle [ 1+ years ago ]  (Read 560 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bb] generic bouncing code by DarkEagle [ 1+ years ago ]
« on: June 29, 2017, 12:28:39 AM »
Title : generic bouncing code
Author : DarkEagle
Posted : 1+ years ago

Description : set up collisions and then use this code with your entity

Code :
Code: BlitzBasic
1. For i = 1 To CountCollisions(entity)
2.         ; Calculate bounce:
3.
4.         ; Get the normal of the surface collided with.
5.         Nx# = CollisionNX#(entity, i)
6.         Ny# = CollisionNY#(entity, i)
7.         Nz# = CollisionNZ#(entity, i)
8.
9.         ; Compute the dot product of the ball's motion vector and the normal of the surface collided with.
10.         VdotN# = xvel#*Nx# + yvel#*Ny# + zvel#*Nz#
11.
12.         ; Calculate the normal force.
13.         NFx# = -2.0 * Nx# * VdotN#
14.         NFy# = -2.0 * Ny# * VdotN#
15.         NFz# = -2.0 * Nz# * VdotN#
16.
17.         ; Add the normal force to the motion vector.
18.         xvel#=(xvel# + NFx#)*bounce#
19.         yvel#=(yvel# + NFy#)*bounce#
20.         zvel#=(zvel# + NFz#)*bounce#
21. Next