January 15, 2021, 05:59:40 PM

### Author Topic: [bb] Homing physics ( 2D ) by Jeppe Nielsen [ 1+ years ago ]  (Read 647 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bb] Homing physics ( 2D ) by Jeppe Nielsen [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : Homing physics ( 2D )
Author : Jeppe Nielsen
Posted : 1+ years ago

Description : Allow enemies to chase player, with velocity and acceleration.

Code :
Code: BlitzBasic
1. ;Homing example, by Jeppe Nielsen 2003
2.
3. Global playerx#
4. Global playery#
5. Global distance#=100
6.
7. Type enemy
8.
9. Field x#,y#
10. Field vx#,vy#
11. Field ax#,ay#
12.
13. Field vmax#
14. Field amax#
15.
16. End Type
17.
18. Graphics3D 800,600,16,2
19.
20. ;create ten enemies at random locations
21. For n=1 To 10
22. ;play with last number
23. ;enemynew(x,y,vmax,amax)
24. enemynew(Rnd(800),Rnd(600),Rnd(0.5,4.5),Rnd(0.08,0.1))
25. Next
26.
27.
28. Repeat
29. Cls
30.
31. Text 10,10,"Move player with mouse"
32. Text 10,30,"LMB - Resize allowed distance to player"
33. Text 10,50,"RMB - Add enemies"
34.
35. If click=0
36.
37.         playerx=MouseX()
38.         playery=MouseY()
39.
40. EndIf
41.
42. If MouseDown(1) And click=0
43.
44.         click=1
45.         clickx=MouseX()
46.         clicky=MouseY()
47.
48. EndIf
49.
50. If MouseDown(1) And click=1
51.
52.         dx=(MouseX()-clickx)
53.         dy=(MouseY()-clicky)
54.
55.         distance#=Sqr(dx*dx+dy*dy)
56.
57. EndIf
58.
59. If MouseDown(1)=False And click=1
60.         click=0
61. EndIf
62.
63. If MouseDown(2)>0
64.         enemynew(Rnd(800),Rnd(600),Rnd(2.5,2.5),Rnd(0.08,0.1))
65. EndIf
66.
67.
68. enemyupdate()
69. enemydraw()
70.
71.
72. Rect playerx-5,playery-5,10,10,0
73.
74. Oval playerx-distance#,playery-distance#,distance#*2,distance#*2,0
75.
76. Flip
77.
78.
79. Until KeyDown(1)
80. End
81.
82. Function enemynew.enemy(x,y,vmax#,amax#)
83.
84. e.enemy=New enemy
85.
86. ex#=x
87. ey#=y
88.
89. evmax#=vmax#
90. eamax#=amax#
91.
92. Return e
93.
94. End Function
95.
96. Function enemyupdate()
97.
98. For e.enemy=Each enemy
99.
100. dx#=(playerx-ex)
101. dy#=(playery-ey)
102.
103. l#=Sqr(dx#*dx#+dy#*dy#)
104.
105. dx#=(dx#/l#)*eamax#
106. dy#=(dy#/l#)*eamax#
107.
108. ;if close enough escape target
109. If l#<=distance#
110. dx#=-dx#
111. dy#=-dy#
112. EndIf
113.
114. ;check against all other enemies, to avoid them
115. dxx#=0
116. dyy#=0
117. co=0
118. For ee.enemy=Each enemy
119.
120.         If ee<>e
121.
122.                 dex#=(ex-eex)
123.                 dey#=(ey-eey)
124.
125.                 l#=Sqr(dex#*dex#+dey#*dey#)
126.
127.                 dxx#=dxx#+(dex#/l#)*eamax#
128.                 dyy#=dyy#+(dey#/l#)*eamax#
129.
130.                 co=co+1
131.         EndIf
132. Next
133.
134. dxx#=dxx#/Float(co)
135. dyy#=dyy#/Float(co)
136.
137. dx#=(dx#+dxx#)/2
138. dy#=(dy#+dyy#)/2
139.
140. eax#=eax#+dx#
141. eay#=eay#+dy#
142.
143. acc#=Sqr(eax#*eax#+eay#*eay#)
144.
145. ;Check if current acceleration is more than allowed
146. If acc#>eamax#
147.
148.         eax#=(eax#/acc#)*eamax
149.         eay#=(eay#/acc#)*eamax
150.
151. EndIf
152.
153. evx#=evx#+eax#
154. evy#=evy#+eay#
155.
156. vel#=Sqr(evx#*evx#+evy#*evy#)
157.
158. ;Check if current velocity is more than allowed
159. If vel#>evmax#
160.         evx#=(evx#/vel#)*evmax
161.         evy#=(evy#/vel#)*evmax
162. EndIf
163.
164. ; add velocity to position
165. ex#=ex#+evx#
166. ey#=ey#+evy#
167.
168. Next
169. End Function
170.
171. Function enemydraw()
172. For e.enemy=Each enemy
173.         Rect ex-3,ey-3,6,6,0
174. Next
175. End Function

TWH(Posted 1+ years ago)

Code: [Select]
`'Blitzmax:Strict'Homing example, by Jeppe Nielsen 2003'mouse position:Global playerx# Global playery#Global distance#=100Global enemylist:TList = CreateList()Type enemy Field x#,y# Field vx#,vy# Field ax#,ay# Field vmax# Field amax# Function enemynew:enemy(x,y,vmax#,amax#) Local e:enemy=New enemy enemylist.addlast(e) e.x#=x e.y#=y e.vmax#=vmax# e.amax#=amax# Return e End Function Function enemydraw() For Local e:enemy=EachIn enemylist SetColor 0,0,255 DrawRect e.x-3,e.y-3,6,6 Next End Function Function enemyupdate() For Local e:enemy=EachIn enemylist Local dx#=(playerx-e.x) Local dy#=(playery-e.y) Local l#=Sqr(dx#*dx#+dy#*dy#) dx#=(dx#/l#)*e.amax# dy#=(dy#/l#)*e.amax# 'If close enough escape target If l#<=distance# dx#=-dx# dy#=-dy# EndIf 'check against all other enemies, To avoid them Local dxx#=0 Local dyy#=0 Local co=0 For Local otherEnemy:enemy=EachIn enemylist If otherEnemy<>e Local dex#=(e.x-otherEnemy.x) Local dey#=(e.y-otherEnemy.y) 'Avoid other enemies. Comment out these 3 lines to remove avoidance. l#=Sqr(dex#*dex#+dey#*dey#) dxx#=dxx#+(dex#/l#)*e.amax# dyy#=dyy#+(dey#/l#)*e.amax# co=co+1 EndIf Next dxx#=dxx#/Float(co) dyy#=dyy#/Float(co) dx#=(dx#+dxx#)/2 dy#=(dy#+dyy#)/2 e.ax#=e.ax#+dx# e.ay#=e.ay#+dy# Local acc#=Sqr(e.ax#*e.ax#+e.ay#*e.ay#) 'Check If current acceleration is more than allowed If acc#>e.amax# e.ax#=(e.ax#/acc#)*e.amax e.ay#=(e.ay#/acc#)*e.amax EndIf e.vx#=e.vx#+e.ax# e.vy#=e.vy#+e.ay# Local vel#=Sqr(e.vx#*e.vx#+e.vy#*e.vy#) 'Check If current velocity is more than allowed If vel#>e.vmax# e.vx#=(e.vx#/vel#)*e.vmax e.vy#=(e.vy#/vel#)*e.vmax EndIf ' add velocity To position e.x#=e.x#+e.vx# e.y#=e.y#+e.vy# Next End FunctionEnd Type'SetGraphicsDriver GLMax2DDriver()Graphics 640,480'Create ten enemies at random locationsLocal n:IntFor n=1 To 10 'play with last number 'enemynew(x,y,vmax,amax) enemy.enemynew(Rnd(800),Rnd(600),Rnd(5,5), 10)'Rnd(0.08,0.1))NextLocal click:Int = 0Local clickx:Int = 0Local clicky:Int = 0Repeat Cls DrawText "Move player with mouse",10,10 DrawText "LMB - Resize allowed distance to player",10,30 DrawText "RMB - Add enemies",10,50 If click=0 playerx=MouseX() playery=MouseY() EndIf If MouseDown(1) And click=0 click=1 clickx=MouseX() clicky=MouseY() EndIf If MouseDown(1) And click=1 Local dx:Int=(MouseX()-clickx) Local dy:Int=(MouseY()-clicky) distance#=Sqr(dx*dx+dy*dy) EndIf If MouseDown(1)=False And click=1 click=0 EndIf If MouseDown(2)>0 enemy.enemynew(Rnd(800),Rnd(600),Rnd(2.5,2.5),Rnd(0.08,0.1)) EndIf enemy.enemyupdate() enemy.enemydraw() 'DrawRect playerx-5,playery-5,10,10 SetAlpha 0.8 SetColor 255,0,0 SetBlend(LIGHTBLEND) DrawOval playerx-distance#,playery-distance#,distance#*2,distance#*2 SetAlpha 1 FlipUntil KeyDown(KEY_ESCAPE) Or AppTerminate()End`

markcw(Posted 1+ years ago)

<a href="../faq/faq_entry0b30.html?id=2" target="_blank">http://www.blitzbasic.com/faq/faq_entry.php?id=2[/url]

Jesse(Posted 1+ years ago)

Dud! I've been here many time and I had not seen the original code. Nice. [/i]