Skip to content

Instantly share code, notes, and snippets.

@assertivist
Created December 12, 2012 00:05
Show Gist options
  • Save assertivist/4263597 to your computer and use it in GitHub Desktop.
Save assertivist/4263597 to your computer and use it in GitHub Desktop.
collision code with sweep test instead of rigid bodies PS it doesn't work
def update(self, dt):
dt = min(dt, 0.2) # let's just temporarily assume that if we're getting less than 5 fps, dt must be wrong.
yaw = self.movement['left'] + self.movement['right']
self.rotate_by(yaw * dt * 60, 0, 0)
walk = self.movement['forward'] + self.movement['backward']
speed = 0
if self.on_ground:
speed = walk
self.xz_velocity = self.position()
#sweep test for destination of walk
#
theta = self.actor.get_h()
mag = speed * dt * 60
cur_pos_ts = TransformState.makePos(self.position())
new_pos = (self.position() - Point3(math.cos(theta), 0, math.sin(theta)) * mag)
new_pos_ts = TransformState.makePos(new_pos)
#print new_pos
sweep_result = self.world.physics.sweepTestClosest(self.hector_capsule_shape, cur_pos_ts, new_pos_ts, BitMask32.all_on(), 0)
#print "s_result : %s" % sweep_result
if sweep_result.has_hit():
hit_position = sweep_result.get_hit_pos()
normal = sweep_result.get_hit_normal()
print "sweep found object at %s , normal %s" % (hit_position, normal)
#reflection = new_pos - normal * (2.0 * new_pos.dot(normal))
#reflection.normalize()
par_dir = normal * new_pos.dot(normal)
perp_dir = new_pos - par_dir
perp_dir.normalize()
perp_component = perp_dir * mag
print "new_pos: %s, par_dir: %s, perp_dir: %s, perp_component: %s" %(new_pos,par_dir,perp_dir,perp_component)
self.move(self.position() - Vec3(perp_component.x, 0, perp_component.z))
#self.move_by(??, 0, ??)
else:
self.move_by(0, 0, mag)
#self.move_by(*move_vector)
self.xz_velocity -= self.position()
self.xz_velocity *= -1
self.xz_velocity /= (dt * 60)
else:
self.move(self.position() + self.xz_velocity * dt * 60)
# Cast a ray from just above our feet to just below them, see if anything hits.
pt_from = self.position() + Vec3(0, 1, 0)
pt_to = pt_from + Vec3(0, -1.1, 0)
result = self.world.physics.ray_test_closest(pt_from, pt_to, MAP_COLLIDE_BIT | SOLID_COLLIDE_BIT)
self.update_legs(walk,dt)
if self.y_velocity <= 0 and result.has_hit():
self.on_ground = True
self.y_velocity = 0
self.move(result.get_hit_pos())
else:
self.on_ground = False
self.y_velocity -= 0.20 * dt
self.move_by(0, self.y_velocity * dt * 60, 0)
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment