BlitzCoder Essentials
•
Home Page
•
About BlitzCoder
•
Contributors
•
Terms of Use
•
Email Us
Main Areas
•
BlitzCoder Chat
•
Discussions
•
Articles/Tutorials
•
Code Database
•
Link Database
•
Showcase Area
•
Worklogs
•
Competitions
Special Areas
•
Undocumented
Other Blitz Sites
•
Blitz Basic Home
•
Blitz Showcase
•
BlitzPlay Library
Forum Login
Username:
Password:
•
Register Now!
BlitzCoder Code Archives Page
Main Codes Page
"real 3d bouncing"
, by darkeagle
really really cool... just enable collisions with the world and any objects you want the ball to bounce with, and let this code do its stuff :) bouncy bouncy!
Code
Const col_world = 1 Const col_ball = 3 Collisions col_ball,col_world,2,2 Collisions col_ball,col_ball,1,2 Type ball Field entity Field xvel#,yvel#,zvel# End Type Const ball_gravity# = 0.05 For i = 1 To 10 CreateBall(Rnd(-10,10),Rnd(-10,10),Rnd(-10,10),Rnd(-0.5,0.5),Rnd(-0.5,0.5),Rnd(-0.5,0.5)) Next Function CreateBall.ball(x#=0,y#=0,z#=0,xvel#=0,yvel#=0,zvel#=0) b.ball = New ball b\entity = CreateSphere() EntityColor b\entity,Rand(28,255),Rand(28,255),Rand(28,255) EntityType b\entity,col_ball PositionEntity b\entity,x,y,z b\xvel# = xvel# b\yvel# = yvel# b\zvel# = zvel# Return b End Function Function UpdateBalls(dt#=1) For b.ball = Each ball For i = 1 To CountCollisions(b\entity) HandleBallBounce(b,i) Next b\yvel = b\yvel - ball_gravity# TranslateEntity b\entity,b\xvel*dt,b\yvel*dt,b\zvel*dt Next End Function Function HandleBallBounce(b.ball,i) ; Calculate bounce: ; Get the normal of the surface collided with. Nx# = CollisionNX#(b\entity, i) Ny# = CollisionNY#(b\entity, i) Nz# = CollisionNZ#(b\entity, i) ; Compute the dot product of the ball's motion vector and the normal of the surface collided with. VdotN# = b\xvel#*Nx# + b\yvel#*Ny# + b\zvel#*Nz# ; Calculate the normal force. NFx# = -2.0 * Nx# * VdotN# NFy# = -2.0 * Ny# * VdotN# NFz# = -2.0 * Nz# * VdotN# ; Add the normal force to the motion vector. b\xvel#=(b\xvel# + NFx#) b\yvel#=(b\yvel# + NFy#) b\zvel#=(b\zvel# + NFz#) End Function
Copyright(c) 2000-2004, BlitzCoder. All Rights Reserved.
Code software created by Krylar's Kreations