Esempio n. 1
0
def RotatePoint3(p, v1, v2):
    v1.normalize()
    v2.normalize()
    cross = v1.cross(v2)
    cross.normalize()
    if cross.length():
        a = v1.angleDeg(v2)
        quat = Quat()
        quat.setFromAxisAngle(a, cross)
        p = quat.xform(p)

    return p
Esempio n. 2
0
    def jump_to (self, pos=None, hpr=None, speed=None):

        if pos is None:
            pos = self.pos(refbody=self.parent)
        if hpr is None:
            hpr = self.hpr(refbody=self.parent)
        if speed is None:
            speed = self.speed(refbody=self.parent)

        self.node.setPos(pos)
        self.node.setHpr(hpr)
        q = Quat()
        q.setHpr(hpr)
        self._vel = Vec3(q.xform(Vec3(0, speed, 0)))
        self._acc = Vec3()
        self._angvel = Vec3()
        self._angacc = Vec3()
        self._prev_vel = Vec3(self._vel)
        self._prev_angvel = Vec3(self._angvel)
Esempio n. 3
0
    def _move (self, dt, rd):

        pos = self._pos
        quat = self._quat
        fvel = self._fvel
        tvel = self._tvel

        termspeed = self._termspeed
        gracc = self.world.gravacc
        fspeed = fvel.length()
        fdir = unitv(fvel)
        absdracc = self.world.absgravacc * (fspeed**2 / termspeed**2)
        if fspeed - absdracc * dt < 0.0:
            absdracc = (fspeed / dt) * 0.5
        dracc = fdir * -absdracc
        facc = gracc + dracc
        #facc = Vec3()
        dfpos = fvel * dt + facc * (0.5 * dt**2)
        fvel1 = fvel + facc * dt

        rollspeed = self._rollspeed
        rollrad = self._rollrad
        if rollspeed != 0.0 and rollrad != 0.0:
            rollspeed1 = rollspeed * (1.0 - rd)**2
            rollrad1 = rollrad * (1.0 - rd)**2
            tdir = unitv(tvel) * (sign(rollspeed * rollrad) or 1)
            fdir1 = unitv(fvel1)
            dsroll = rollspeed1 * dt
            dtquat = Quat()
            dtquat.setFromAxisAngleRad(dsroll, fdir)
            tdir1p = Vec3(dtquat.xform(tdir))
            tdir1 = unitv(fdir1.cross(tdir1p).cross(fdir1))
            tspeed1 = rollspeed1 * rollrad1
            dtpos = tvel * dt
            tvel1 = tdir1 * tspeed1
        else:
            dtpos = Vec3()
            dtquat = Quat()
            tvel1 = Vec3()

        pos1 = pos + dfpos + dtpos
        self.node.setPos(pos1)

        fdir1 = unitv(fvel1)
        paxis = fdir.cross(fdir1)
        if paxis.length() > 1e-5:
            paxis.normalize()
            dspitch = fdir.signedAngleRad(fdir1, paxis)
        else:
            paxis = quat.getRight()
            paxis.normalize()
            dspitch = 0.0
        dfquat = Quat()
        dfquat.setFromAxisAngleRad(dspitch, paxis)
        quat1 = quat * dfquat * dtquat
        self.node.setQuat(quat1)

        self._pos = pos1
        self._quat = quat1
        self._fvel = fvel1
        self._tvel = tvel1
Esempio n. 4
0
    def __init__ (self, world,
                  family, species, hitforce,
                  name, side,
                  mass=None,
                  pos=Point3(), hpr=Vec3(),
                  vel=Vec3(), angvel=Vec3(),
                  acc=Vec3(), angacc=Vec3(),
                  modeldata=None,
                  hitboxdata=[], hitboxcritdata=[],
                  hitinto=True, hitfrom=True,
                  sensordata=None,
                  center=None, bbox=None, bboxcenter=None,
                  hitlight=None, hitdebris=None, hitflash=None,
                  passhitfx=False,
                  amblit=False, dirlit=False, pntlit=0, fogblend=False,
                  obright=False, ltrefl=False,
                  shdshow=False, shdmodind=None,
                  parent=None):

        self.alive = True

        self.world = world
        self.family = family
        self.species = species
        self.name = name
        self.side = side
        self.parent = parent or world
        self.mass = mass
        self.hitforce = hitforce
        self.hitlight = hitlight
        self.hitdebris = hitdebris
        self.hitflash = hitflash
        self.passhitfx = passhitfx

        self.racc = Vec3()
        self.aacc = Vec3()
        self.rangacc = Vec3()
        self.aangacc = Vec3()

        if isinstance(vel, (float, int)):
            q = Quat()
            q.setHpr(hpr)
            vel = q.xform(Vec3(0, vel, 0))

        self._acc = Vec3(acc)
        self._angacc = Vec3(angacc)
        self._vel = Vec3(vel)
        self._prev_vel = Vec3(vel)
        self._angvel = Vec3(angvel)
        self._prev_angvel = Vec3(angvel)

        self.birth_time = world.time

        self.initiator = None

        arg_bbox = bbox
        arg_bboxcenter = bboxcenter

        self.node = self.parent.node.attachNewNode(self.name)
        if modeldata and modeldata.path:
            if ltrefl:
                glossmap = modeldata.glossmap
                if not glossmap:
                    glossmap = "images/_glossmap_none.png"
            else:
                glossmap = None
            if isinstance(modeldata.glowmap, Vec4):
                glowcol = modeldata.glowmap
                glowmap = None
            else:
                glowcol = None
                glowmap = modeldata.glowmap
            if modeldata.clamp is not None:
                clamp = modeldata.clamp
            else:
                clamp = True
            shadowmap = base.world_shadow_texture if shdshow else None
            if isinstance(modeldata.path, (tuple, list)):
                if (len(modeldata.path) == 2 and
                    isinstance(modeldata.path[0], NodePath) and
                    isinstance(modeldata.path[1], str)):
                    pnode, tname = modeldata.path
                    ret = extract_model_lod_chain(pnode, tname,
                        texture=modeldata.texture, normalmap=modeldata.normalmap,
                        glowmap=glowmap, glossmap=glossmap,
                        shadowmap=shadowmap,
                        clamp=clamp)
                    lnode, models, fardists, bbox, bcen, ppos, phpr = ret
                    if pos is None:
                        pos = ppos
                    if hpr is None:
                        hpr = phpr
                    for model in models:
                        model.setPos(Point3())
                        model.setHpr(Vec3())
                else:
                    ret = load_model_lod_chain(
                        world.vfov, modeldata.path,
                        texture=modeldata.texture, normalmap=modeldata.normalmap,
                        glowmap=glowmap, glossmap=glossmap,
                        shadowmap=shadowmap,
                        clamp=clamp, scale=modeldata.scale,
                        pos=modeldata.offset, hpr=modeldata.rot)
                    lnode, models, fardists, bbox, bcen = ret
                self.modelnode = lnode
                self.models = models
                self.fardists = fardists
                self.bbox = bbox
                self.bboxcenter = bcen
                lnode.reparentTo(self.node)
            else:
                model = load_model(
                    modeldata.path,
                    texture=modeldata.texture, normalmap=modeldata.normalmap,
                    glowmap=glowmap, glossmap=glossmap,
                    shadowmap=shadowmap,
                    clamp=clamp, scale=modeldata.scale,
                    pos=modeldata.offset, hpr=modeldata.rot)
                self.modelnode = model
                self.models = [model]
                self.fardists = [-1.0]
                model.reparentTo(self.node)
                if not modeldata.nobbox:
                    bmin, bmax = model.getTightBounds()
                    self.bbox = bmax - bmin
                    self.bboxcenter = (bmin + bmax) * 0.5
                else:
                    self.bbox = Vec3()
                    self.bboxcenter = Vec3()
        else:
            self.modelnode = None
            self.models = []
            self.fardists = []
            self.bbox = Vec3()
            self.bboxcenter = Vec3()
        if arg_bbox is not None:
            self.bbox = arg_bbox
            if arg_bboxcenter is not None:
                self.bboxcenter = arg_bboxcenter
            else:
                self.bboxcenter = Vec3()
        elif arg_bboxcenter is not None:
            self.bboxcenter = arg_bboxcenter
        self.update_bbox(bbox=self.bbox, bboxcenter=self.bboxcenter)
        self.node.setPos(pos)
        self.node.setHpr(hpr)

        self.hitboxes = []
        self.set_hitboxes(hitboxdata, hitboxcritdata, hitinto, hitfrom)
        #for hbx in self.hitboxes:
            #hbx.cnode.show()
        self.inert_collide = False
        self.hits_critical = True

        if center is not None:
            self.center = center
        elif self.hitboxes:
            self.center = Point3()
            volume = 0.0
            for hbx in self.hitboxes:
                self.center += hbx.center * hbx.volume
                volume += hbx.volume
            self.center /= volume
        else:
            self.center = self.bboxcenter

        if self.hitlight:
            self._init_hitlight()
        if self.hitdebris:
            self._init_hitdebris()
        if self.hitflash:
            self._init_hitflash()

        # Before shader is set.
        if base.with_world_shadows:
            if modeldata and modeldata.shadowpath:
                model = load_model(
                    modeldata.shadowpath,
                    scale=modeldata.scale,
                    pos=modeldata.offset, hpr=modeldata.rot)
                model.reparentTo(world.shadow_node)
                self.shadow_node = model
            elif self.models and shdmodind is not None:
                model = self.models[shdmodind]
                self.shadow_node = model.copyTo(world.shadow_node)
            else:
                self.shadow_node = None

        if self.models:
            shdinp = world.shdinp
            ambln = shdinp.ambln if amblit else None
            dirlns = shdinp.dirlns if dirlit else []
            #pntlit = 0
            pntlns = shdinp.pntlns[:pntlit]
            fogn = shdinp.fogn if fogblend else None
            camn = shdinp.camn
            pntobrn = shdinp.pntobrn if obright else None
            normal = modeldata.normalmap is not None
            gloss = ltrefl
            glow = glowcol if glowcol is not None else (glowmap is not None)
            shadowrefn = shdinp.shadowrefn if shdshow else None
            shadowdirlin = shdinp.shadowdirlin if shdshow else None
            shadowblendn = shdinp.shadowblendn if shdshow else None
            shadowpush = 0.003 if shdshow else None
            shadowblur = SHADOWBLUR.NONE if shdshow else None
            showas = False
            #showas = "%s-shader" % (self.family)
            ret = make_shader(
                ambln=ambln, dirlns=dirlns, pntlns=pntlns,
                fogn=fogn, camn=camn, pntobrn=pntobrn,
                normal=normal, gloss=gloss, glow=glow,
                shadowrefn=shadowrefn, shadowdirlin=shadowdirlin,
                shadowblendn=shadowblendn, shadowpush=shadowpush,
                shadowblur=shadowblur,
                showas=showas, getargs=True)
            self.shader, self.shader_kwargs = ret
            self.modelnode.setShader(self.shader)
        self.pntlit = pntlit

        if sensordata is not None:
            self.sensorpack = SensorPack(parent=self,
                                         scanperiod=sensordata.scanperiod,
                                         relspfluct=sensordata.relspfluct,
                                         maxtracked=sensordata.maxtracked)
        else:
            self.sensorpack = None

        self.shotdown = False
        self.retreating = False
        self.outofbattle = False
        self.controlout = False

        self._last_collide_body = None
        self._recorded_as_kill = False

        if not base.with_sound_doppler:
            self.ignore_flyby = 0

        world.register_body(self)

        base.taskMgr.add(self._loop_fx, "body-effects-%s" % self.name)