March 01, 2021, 10:47:19 PM

Author Topic: [bb] 2d ReboundCollide() function by aab [ 1+ years ago ]  (Read 419 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] 2d ReboundCollide() function by aab [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : 2d ReboundCollide() function
Author : aab
Posted : 1+ years ago

Description : input the object(image) masses and speeds, and watch them whack each other accordingly. It can be used repeatedly throughout a program without having to repeat the code!

The images (and related co-ordinates) are '1' and '2'

The values 'a' and 'b' are used within the function to demonstrate that it can be used for multiple images and their related co-ordinates (it works via the co-ordinates really)

in order for this to work, the co-ordinates used MUST be global in order for them to be replaced within the function (and preferebly they should be FLOATs.~~What is happenening is that the parameters passed(x1,x2 etc) in this case, temporarily replace xa and
xb, which are then altered within the function, which will then, depending apon the FLAG parameter you input will replace the co-ordinates you input with the shifted ones.

The one thing i resent about this though, is the need for the flag parameter, and for u to have to specify the flag, and edit the 'select' statement within the function to allow different output conditions. ~(For those of you whove used B3d its just like specifying Entitytype, then using 'Countcollisions' for those Entitytypes)

This command in itself should be usefull in using collissions to stop eg:enemies walking through each other, and right through your guy, and for heavy blocks to push(ie:give larger mass# values) and even for ground, by using masses of say 10000 etc. mass of course, (as well as speed) should not fall below 0.0001 (or even 1 in the case of mass), and should not be in excess of 1x(10^7)

how to use
         Basically, input the masses and bounce-speeds of the images 'objects', as well as their co-ordinates and handles, and the flag parameter which specifies the co-ordinates to be affected, and you can use this command for multiple collissins throughout a program.
         The function returns TRUE if the images do 'rebound collide' and so you could use this in replace for imagesoverlap(,,,,)=true, and it will bounce the images off each other as well.
     I dont believe its very magical at all in terms of speed, but it makes ur life a little bit easier.


Code :
Code: BlitzBasic
  1. ;Rebound Collide
  2. Graphics 400,300
  3. SetBuffer BackBuffer()
  4. SeedRnd MilliSecs()
  5. AutoMidHandle True
  6.  
  7.  
  8. speed1#=1
  9. speed2#=1
  10.  
  11. image_1=LoadImage( "charsbbdrag.png" )
  12. Global x1#=0
  13. Global y1#=0
  14. image_2=LoadImage( "charsbbship.bmp" )
  15. Global x2#=200
  16. Global y2#=150
  17. mass1#=1
  18. mass2#=10
  19. If image_1=0 Or image_2=0 Then RuntimeError "Image Unfound!"
  20.  
  21. While Not KeyHit(1)
  22. Cls
  23.  
  24.  
  25.  
  26. reboundcollide#(image_1,x1#,y1#,frame1,mass1#,speed1#,image_2,x2#,y2#,frame2,mass2#,speed2#,1)
  27.  
  28. DrawImage image_1,x1#,y1#
  29. DrawImage image_2,x2#,y2#
  30.  
  31. If KeyDown(203) x1#=x1#-speed1#
  32. If KeyDown(205) x1#=x1#+speed1#
  33. If KeyDown(200) y1#=y1#-speed1#
  34. If KeyDown(208) y1#=y1#+speed1#
  35.  
  36. If KeyDown(75) x2#=x2#-speed2#
  37. If KeyDown(77) x2#=x2#+speed2#
  38. If KeyDown(72) y2#=y2#-speed2#
  39. If KeyDown(80) y2#=y2#+speed2#
  40.  
  41.  
  42. If KeyDown(16) speed1#=speed1#-0.1
  43. If KeyDown(17) speed1#=speed1#+0.1
  44. If speed#<0.1 speed#=0.1
  45. If speed#>10 speed#=10
  46. Text 0,0,("S1: "+Int(speed1#)+"S2: "+Int(speed2#))
  47. Text 0,10,(Int(x1)+","+Int(y1))
  48. Text 0,20,(Int(x2)+","+Int(y2))
  49.  
  50. Text 0,50,(reboundcollide#(image_1,x1#,y1#,frame1,mass1#,speed1#,image_2,x2#,y2#,frame2,mass2#,speed2#,1))
  51.  
  52. VWait:Flip False
  53. Wend
  54. End
  55.  
  56. Function reboundcollide#(image_a,xa#,ya#,framea,massa#,speeda#,image_b,xb#,yb#,frameb,massb#,speedb#,flag)
  57.         Text 0,30,(Int(xa#)+","+Int(ya#))
  58.         Text 0,40,(Int(xb#)+","+Int(yb#))
  59.         If ImagesCollide(image_a,xa#,ya#,framea,image_b,xb#,yb#,frameb)
  60.                 If massa#=0 Or massb#=0 Then RuntimeError "0 Mass? that object can't exist! try 0.00001!"
  61.                 If image_a=0 Or image_b=0 Then RuntimeError "Image Handle unparameterised!"
  62.                 xa#=xa#+(Sgn(xa#-xb#)*speeda#)/massa#
  63.                 xb#=xb#+(Sgn(xb#-xa#)*speedb#)/massb#
  64.                 ya#=ya#+(Sgn(ya#-yb#)*speeda#)/massa#
  65.                 yb#=yb#+(Sgn(yb#-ya#)*speedb#)/massb#
  66.                 Select flag ;this is the part u must annotate for each possible co-ord situation:the FLAG!
  67.                         Case 1 x1#=xa# x2#=xb# y1#=ya# y2#=yb#
  68.                 End Select
  69.                 If Not massa#=0 And massb#=0 Then Return True
  70.         EndIf
  71.         Return False
  72. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal