鱼C论坛

 找回密码
 立即注册
查看: 446|回复: 1

[已解决]cocos2d python 画一个圆在pymunk重力下自由下落

[复制链接]
发表于 2024-11-18 14:05:12 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
在cocos2d python界面画一个圆,在pymunk重力下自由下落,以下代码如何修改?
  1. # coding: utf-8
  2. import cocos
  3. import pymunk
  4. from pyglet.gl import *
  5. from math import cos, tan, pi
  6. class Hello(cocos.layer.Layer):
  7.     def __init__(self):
  8.         super(Hello, self).__init__()
  9.         self.ball_body, self.ball_shape = create_ball(self)
  10.         self.space.add(self.ball_body, self.ball_shape)

  11.         # 创建Pymunk空间
  12.         self.space = pymunk.Space()
  13.         self.space.gravity = (0, -300)
  14.         self.elapsed = 0
  15.         self.schedule(self.update)

  16.         # 创建圆的Pymunk body和shape
  17.         radius = 20
  18.         mass = 1000
  19.         moment = pymunk.moment_for_circle(mass, 0, radius)
  20.         self.cbody = pymunk.Body(mass, moment)
  21.         self.cbody.position = 300, 300
  22.         self.c = pymunk.Circle(self.cbody, radius)
  23.         self.space.add(self.cbody, self.c)

  24.     def create_ball(self):
  25.         super().draw()
  26.         self.circle_center = (300, 300)
  27.         self.circle_radius = 20
  28.         # 设置颜色为绿色并绘制圆
  29.         glColor4ub(0, 255, 0, 255)
  30.         num_segments = 100
  31.         theta = 2 * pi / num_segments
  32.         tangetial_factor = tan(theta)  # Calculate the tangential factor
  33.         radial_factor = cos(theta)  # Calculate the radial factor
  34.         x = self.circle_radius  # Start at angle = 0
  35.         y = 0
  36.         glBegin(GL_LINE_LOOP)
  37.         for i in range(num_segments):
  38.             glVertex2f(x + self.circle_center[0], y + self.circle_center[1])
  39.             tx = -y
  40.             ty = x
  41.             x += tx * tangetial_factor
  42.             y += ty * tangetial_factor
  43.             x *= radial_factor
  44.             y *= radial_factor
  45.         glEnd()

  46.         self.ball_body = pymunk.Body(body_type=pymunk.Body.KINEMATIC)
  47.         self.ball_shape = pymunk.Poly(self.ball_body, self.circle)
  48.         self.ball_shape.friction = 0.5
  49.         self.ball_shape.collision_type = 1
  50.         self.ball_shape.density = 0.1
  51.         return self.ball_body, self.ball_shape

  52.     def update(self, dt):
  53.         self.space.step(dt)
  54.         self.ball.position = self.cbody.position

  55. if __name__ == "__main__":
  56.     cocos.director.director.init()
  57.     hello_layer = Hello()
  58.     main_scene = cocos.scene.Scene(hello_layer)
  59.     cocos.director.director.run(main_scene)
复制代码
最佳答案
2024-11-18 14:13:53
要在 Cocos2d-python 中使用 Pymunk 实现一个圆在重力下自由下落,您需要正确创建和更新圆的物理表现和显示。以下是对您提供的代码的修正,以确保圆正确地在重力影响下下落:

  1. import cocos
  2. import pymunk
  3. from pyglet.gl import *
  4. from math import cos, tan, pi

  5. class Hello(cocos.layer.Layer):
  6.     def __init__(self):
  7.         super(Hello, self).__init__()

  8.         # 创建Pymunk空间
  9.         self.space = pymunk.Space()
  10.         self.space.gravity = (0, -300)

  11.         # 创建圆的Pymunk body和shape
  12.         radius = 20
  13.         mass = 10  # 质量设为较小的值以便测试
  14.         moment = pymunk.moment_for_circle(mass, 0, radius)
  15.         self.cbody = pymunk.Body(mass, moment)
  16.         self.cbody.position = (300, 300)
  17.         self.c = pymunk.Circle(self.cbody, radius)
  18.         self.c.friction = 0.5
  19.         self.c.elasticity = 0.9  # 加入一点弹性
  20.         self.space.add(self.cbody, self.c)

  21.         self.schedule(self.update)

  22.     def draw(self):
  23.         # 设置颜色为绿色并绘制圆
  24.         glColor4ub(0, 255, 0, 255)
  25.         num_segments = 100
  26.         theta = 2 * pi / num_segments
  27.         tangetial_factor = tan(theta)  # 计算切线因子
  28.         radial_factor = cos(theta)  # 计算径向因子

  29.         x = self.c.radius  # 起始点为角度0
  30.         y = 0

  31.         glPushMatrix()
  32.         glTranslatef(self.cbody.position.x, self.cbody.position.y, 0)
  33.         glBegin(GL_LINE_LOOP)
  34.         for i in range(num_segments):
  35.             glVertex2f(x, y)
  36.             tx = -y
  37.             ty = x
  38.             x += tx * tangetial_factor
  39.             y += ty * tangetial_factor
  40.             x *= radial_factor
  41.             y *= radial_factor
  42.         glEnd()
  43.         glPopMatrix()

  44.     def update(self, dt):
  45.         self.space.step(dt)

  46. if __name__ == "__main__":
  47.     cocos.director.director.init()
  48.     hello_layer = Hello()
  49.     main_scene = cocos.scene.Scene(hello_layer)
  50.     cocos.director.director.run(main_scene)
复制代码


### 修改要点:
1. **创建Pymunk Body的类型和设置**:我将body的类型设置为动态(去掉了KINEMATIC类型),它会自然地受到重力影响。
2. **正确绘制圆**:确保每次绘制时都是以 `self.cbody.position` 作为中心来进行绘制,以体现下落效果。
3. **物理属性**:调整了弹性(elasticity)和摩擦力(friction),以简单模拟现实效果。
4. **删除冗余代码**:去掉了不必要的代码和重复定义的函数参数。

这些修改确保了圆的物理行为和图形显示能够同步,即圆能够因重力而下落并在屏幕上看到其移动。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-11-18 14:13:53 | 显示全部楼层    本楼为最佳答案   
要在 Cocos2d-python 中使用 Pymunk 实现一个圆在重力下自由下落,您需要正确创建和更新圆的物理表现和显示。以下是对您提供的代码的修正,以确保圆正确地在重力影响下下落:

  1. import cocos
  2. import pymunk
  3. from pyglet.gl import *
  4. from math import cos, tan, pi

  5. class Hello(cocos.layer.Layer):
  6.     def __init__(self):
  7.         super(Hello, self).__init__()

  8.         # 创建Pymunk空间
  9.         self.space = pymunk.Space()
  10.         self.space.gravity = (0, -300)

  11.         # 创建圆的Pymunk body和shape
  12.         radius = 20
  13.         mass = 10  # 质量设为较小的值以便测试
  14.         moment = pymunk.moment_for_circle(mass, 0, radius)
  15.         self.cbody = pymunk.Body(mass, moment)
  16.         self.cbody.position = (300, 300)
  17.         self.c = pymunk.Circle(self.cbody, radius)
  18.         self.c.friction = 0.5
  19.         self.c.elasticity = 0.9  # 加入一点弹性
  20.         self.space.add(self.cbody, self.c)

  21.         self.schedule(self.update)

  22.     def draw(self):
  23.         # 设置颜色为绿色并绘制圆
  24.         glColor4ub(0, 255, 0, 255)
  25.         num_segments = 100
  26.         theta = 2 * pi / num_segments
  27.         tangetial_factor = tan(theta)  # 计算切线因子
  28.         radial_factor = cos(theta)  # 计算径向因子

  29.         x = self.c.radius  # 起始点为角度0
  30.         y = 0

  31.         glPushMatrix()
  32.         glTranslatef(self.cbody.position.x, self.cbody.position.y, 0)
  33.         glBegin(GL_LINE_LOOP)
  34.         for i in range(num_segments):
  35.             glVertex2f(x, y)
  36.             tx = -y
  37.             ty = x
  38.             x += tx * tangetial_factor
  39.             y += ty * tangetial_factor
  40.             x *= radial_factor
  41.             y *= radial_factor
  42.         glEnd()
  43.         glPopMatrix()

  44.     def update(self, dt):
  45.         self.space.step(dt)

  46. if __name__ == "__main__":
  47.     cocos.director.director.init()
  48.     hello_layer = Hello()
  49.     main_scene = cocos.scene.Scene(hello_layer)
  50.     cocos.director.director.run(main_scene)
复制代码


### 修改要点:
1. **创建Pymunk Body的类型和设置**:我将body的类型设置为动态(去掉了KINEMATIC类型),它会自然地受到重力影响。
2. **正确绘制圆**:确保每次绘制时都是以 `self.cbody.position` 作为中心来进行绘制,以体现下落效果。
3. **物理属性**:调整了弹性(elasticity)和摩擦力(friction),以简单模拟现实效果。
4. **删除冗余代码**:去掉了不必要的代码和重复定义的函数参数。

这些修改确保了圆的物理行为和图形显示能够同步,即圆能够因重力而下落并在屏幕上看到其移动。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-8 05:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表