Ooops
January 15, 2021, 06:30:25 PM

Author Topic: [bb] Automatic 2D collision response by BlitzSupport [ 1+ years ago ]  (Read 442 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Automatic 2D collision response
Author : BlitzSupport
Posted : 1+ years ago

Description : I thought I'd posted this already, but don't see it anywhere...

It only does a sort of circular response to the collision, but should be useful for something! It's also pretty fixed as-is, so just use it as a starting point for your own needs. Replace "big-moo.bmp" with an image of your own...


Code :
Code: BlitzBasic
  1. ; -----------------------------------------------------------------------------
  2. ; Automatic collision response in 2D...
  3. ; -----------------------------------------------------------------------------
  4. ; support@blitzbasic.com
  5. ; -----------------------------------------------------------------------------
  6.  
  7. Graphics 640, 480
  8. AppTitle "Automatic 2D collision response"
  9. SetBuffer BackBuffer ()
  10. ClsColor 64, 128, 180
  11.  
  12. ; -----------------------------------------------------------------------------
  13. ; Load images...
  14. ; -----------------------------------------------------------------------------
  15.  
  16. sheep1 = LoadImage ("big-moo.bmp")
  17. MaskImage sheep1, 255, 0, 255
  18. sheep2 = CopyImage (sheep1)
  19.  
  20. px = 10: py = 10 ; Player x and y positions
  21.  
  22. ; -----------------------------------------------------------------------------
  23. ; The variables that get the resulting movement values must be floats!
  24. ; -----------------------------------------------------------------------------
  25.  
  26. cx# = 90: cy# = 90 ; Computer x and y positions
  27.  
  28. ; -----------------------------------------------------------------------------
  29. ; The 'mover' variable specifies how much to move in a collision. You could
  30. ; make this dynamic within your main loop, of course...
  31. ; -----------------------------------------------------------------------------
  32.  
  33. mover# = 1
  34.  
  35. Repeat
  36.  
  37.         Cls
  38.  
  39.         If KeyDown (203) px = px - 1
  40.         If KeyDown (205) px = px + 1
  41.         If KeyDown (200) py = py - 1
  42.         If KeyDown (208) py = py + 1
  43.        
  44.         If ImagesCollide (sheep1, px, py, 0, sheep2, cx, cy, 0)
  45.                 angle# = ATan2 (cy - py, cx - px)
  46.                 cx = cx + (mover * (Cos (angle)))
  47.                 cy = cy + (mover * (Sin (angle)))
  48.         EndIf
  49.        
  50.         DrawImage sheep1, px, py
  51.         DrawImage sheep2, cx, cy
  52.                
  53.         Flip
  54.        
  55. Until KeyHit (1)
  56.  
  57. End


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal