update box2d

This commit is contained in:
blueloveTH 2023-11-10 14:47:12 +08:00
parent bfc5e82e0e
commit 12f6617bee
3 changed files with 86 additions and 6 deletions

View File

@ -52,13 +52,13 @@ struct PyContactListener: b2ContactListener{
void _contact_f(b2Contact* contact, StrName name); void _contact_f(b2Contact* contact, StrName name);
void BeginContact(b2Contact* contact) override { void BeginContact(b2Contact* contact) override {
DEF_SNAME(on_contact_begin); DEF_SNAME(on_box2d_contact_begin);
_contact_f(contact, on_contact_begin); _contact_f(contact, on_box2d_contact_begin);
} }
void EndContact(b2Contact* contact) override { void EndContact(b2Contact* contact) override {
DEF_SNAME(on_contact_end); DEF_SNAME(on_box2d_contact_end);
_contact_f(contact, on_contact_end); _contact_f(contact, on_box2d_contact_end);
} }
}; };

View File

@ -43,6 +43,8 @@ https://github.com/blueloveTH/pocketpy/blob/main/include/typings/box2d.pyi
## Example ## Example
### Simple simulation
```python ```python
import box2d import box2d
from linalg import vec2 from linalg import vec2
@ -95,3 +97,79 @@ for i in range(30):
28 vec2(0.967, 0.000) 28 vec2(0.967, 0.000)
29 vec2(1.000, 0.000) 29 vec2(1.000, 0.000)
``` ```
### Two bodies with collision
```python
import box2d
from linalg import vec2
world = box2d.World()
# body_a is a static body with a box shape at (1, 0)
body_a = box2d.Body(world)
body_a.set_box_shape(0.5, 0.5)
body_a.position = vec2(9, 0)
class Node:
def on_box2d_contact_begin(self, other):
print('body_a collides with body_b!!')
def on_box2d_pre_step(self):
pass
def on_box2d_post_step(self):
pass
"""
B A
-|-|-|-|-|-|-|-|-|-
0 1 2 3 4 5 6 7 8 9
"""
# body_b is a dynamic body with a circle shape at (0, 0)
body_b = box2d.Body(world, Node())
body_b.set_circle_shape(0.5)
body_b.position = vec2(0, 0)
# give body_b a velocity and it will collide with body_a at some point
body_b.velocity = vec2(12, 0)
for i in range(30):
world.step(1/30, 8, 3)
print(i, body_b.position)
```
```
0 vec2(0.400, 0.000)
1 vec2(0.800, 0.000)
2 vec2(1.200, 0.000)
3 vec2(1.600, 0.000)
4 vec2(2.000, 0.000)
5 vec2(2.400, 0.000)
6 vec2(2.800, 0.000)
7 vec2(3.200, 0.000)
8 vec2(3.600, 0.000)
9 vec2(4.000, 0.000)
10 vec2(4.400, 0.000)
11 vec2(4.800, 0.000)
12 vec2(5.200, 0.000)
13 vec2(5.600, 0.000)
14 vec2(6.000, 0.000)
15 vec2(6.400, 0.000)
16 vec2(6.800, 0.000)
17 vec2(7.200, 0.000)
18 vec2(7.600, 0.000)
19 vec2(8.000, 0.000)
body_a collides with body_b!!
20 vec2(8.175, 0.000)
21 vec2(8.351, 0.000)
22 vec2(8.527, 0.000)
23 vec2(8.702, 0.000)
24 vec2(8.878, 0.000)
25 vec2(9.054, 0.000)
26 vec2(9.230, 0.000)
27 vec2(9.405, 0.000)
28 vec2(9.581, 0.000)
29 vec2(9.757, 0.000)
```

View File

@ -2,8 +2,10 @@ from linalg import vec2, vec4
from typing import Iterable from typing import Iterable
class _NodeLike: # duck-type protocol class _NodeLike: # duck-type protocol
def on_contact_begin(self, other: 'Body'): ... def on_box2d_contact_begin(self, other: 'Body'): ...
def on_contact_end(self, other: 'Body'): ... def on_box2d_contact_end(self, other: 'Body'): ...
def on_box2d_pre_step(self): ...
def on_box2d_post_step(self): ...
class _DrawLike: # duck-type protocol class _DrawLike: # duck-type protocol
def draw_polygon(self, vertices: list[vec2], color: vec4): ... def draw_polygon(self, vertices: list[vec2], color: vec4): ...