News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

Realistic 2d bouncing ball reactions help please.

Started by LemonWizard, April 07, 2014, 01:10:23 AM

Previous topic - Next topic

LemonWizard

Hey so here's the code I have. I tried to use the angle of the slope which is a 1D array that follows the X plane.
The Y plane is inside the X plane so far each X, the Y is given.
A really simple thing I've used before for creating slopes in 2D


Here's the code.


; PROJECT : ball
; AUTHOR  : LemonWizard
; CREATED : 4/6/2014
; EDITED  : 4/6/2014
; ---------------------------------------------------------------------
SETFPS 60
dim slope(getscreenwidth() )



ballvelocityx=0
ballvelocityy=0

gravity=1


do

if leftkey()
ballvelocityx=ballvelocityx-1
endif

if rightkey()
ballvelocityx=ballvelocityx+1
endif

if ballvelocityx>4 then ballvelocityx=4
if ballvelocityx<neg(4) then ballvelocityx=neg(4)

if ballvelocityy>25 then ballvelocityy=25
if ballvelocityy<neg(25) then ballvelocityy=neg(25)


if leftmousebutton()
slope(mousex())=mousey()
slope(mousex()+1)=mousey()
slope(mousex()+2)=mousey()
slope(mousex()+2)=mousey()

if mousex()>3
slope(mousex()-1)=mousey()
slope(mousex()-2)=mousey()
slope(mousex()-3)=mousey()
endif



endif



for t=0 to getscreenwidth()

x=t
y=slope(x)
ink rgb(255, 0, 0)


box x, y, x+2, y+2, 1

next t

if balldropped=false
ballx=mousex()
bally=mousey()
endif

ink rgb(0, 255, 100)
circle ballx, bally, 18, 1

if rightmousebutton()
balldropped=true
endif

if balldroppped=true

if skip=true then goto ski:
if skip=false
ballvelocityy=ballvelocityy+gravity
skip=true
endif
endif



ski:


ballx=ballx+ballvelocityx
bally=bally+ballvelocityy
bally=bally+gravity

if ballvelocityx<0
ballx=ballx+neg(cos(angle#))
endif

if ballvelocityx>0
ballx=ballx+cos(angle#)
endif


//bally=bally+sin(angle#)
if gravity=0
ballvelocityy=ballvelocityy+1
endif

if bally>slope(ballx+9) then bally=slope(ballx+1)
if bally=>slope(ballx+9)
xang=slope(ballx)
yang=slope(ballx+8)


angle#=getangle2d( ballx, slope(ballx+9), ballx, slope(ballx+8))
ballvelocityx=neg(ballvelocityx)
ballvelocityy=neg((ballvelocityy/2))
gravity=0
if ballvelocityx=0 then ballvelocityx=1
endif


if ballx<0 then ballvelocityx=neg(ballvelocityx)
if ballx>getscreenwidth() then ballvelocityx=neg(ballvelocityx)
if bally<0
ballvelocityy=neg(ballvelocityy)
bally=bally+12
endif




sync
cls rgb(0,0,0)



loop






I probably could have done this better, gravity is only used once really. >.> it's just initial
Test it and tell me what I did wrong maybe (someone can?) thanks in advance :3
I love doing stuff like this


kevin

#2
 in the project packs & help files (Projects/Examples/Worlds/) there's a bunch of examples that show bouncing, one's called Worlds_Closest_Point