Laser Beam using Mesh Drawer Won't Render?

do you mean like this:

 def handleShift(self, arg):
        if arg == 1:
            self.source = self.pandaActor
            self.target = NodePath(PandaNode("target"))
            self.target.reparentTo(self.pandaActor)
            self.target.setY(5)
            self.createBeam(self.source, self.target)
        else:
            self.deleteBeam()

    def createBeam(self, source, target):
        self.generator = MeshDrawer()
        self.generator.setBudget(1000)
        self.generatorNode = self.generator.getRoot()
        self.generatorNode.reparentTo(self.render)
        beamtex = self.loader.loadTexture("/Users/38167/PycharmProjects/viren/venv/wood.jpg")
        self.generatorNode.setTexture(beamtex)
        self.generatorNode.setTransparency(True)
        self.generatorNode.setPos(-0.3, 2, 2)
        self.generatorNode.setDepthWrite(False)
        self.generatorNode.setTwoSided(True)
        self.generatorNode.setLightOff(True)
        self.taskMgr.add(self.drawBeam, "drawBeamTask", extraArgs=[source, target], appendTask=True)

    def drawBeam(self, source, target, task):
        t = globalClock.getFrameTime()
        self.generator.begin(base.cam, self.render)
        self.generator.segment(Vec3(self.source.getPos()), Vec3(self.target.getPos()), Vec4(0, 0, 1, 1), 0.3, Vec4(1, 1, 1, 1))
        
        self.generator.end()

        return task.cont

    def deleteBeam(self):
        self.generatorNode.removeNode()
        self.taskMgr.remove("drawBeamTask")

btw thanks for helping me, I know it can be difficult sometimes🙏

Hmm… You have the right basic idea, I do believe.

However, there is one issue: you’re creating your target NodePath over and over and over again with each pres of the “shift” key, if I’m understanding your code correctly. And since you’re never destroying them, they’ll accumulate, and may even, with enough key-presses, become a performance issue.

If you really want to keep re-creating your objects like this, then I recommend that you be careful that you also destroy them afterwards.

Ah yeah I got to fix that. Also, did that do the trick? When I ran it it kinda just went in the same direction whenever I turned, :man_shrugging:

Thanks!

When you say “in the same direction”, do you mean that it didn’t turn with your character, or that it did turn with your character?

Screen Shot 2019-12-13 at 10.15.56 PM

what I was trying to say

I… don’t really know what I’m looking at there, I’m afraid. ^^;

I see a panda, and… something else intersecting it? And a long brown rectangle, which I assume is your laser.

Perhaps an animated gif would be more demonstrative of the problem? Or a more-detailed description?

oh don’t worry about the other actor, that’s the thing intersecting. The beam is the long brown rectangle, as you can see it’s directed someplace else. I thought it was supposed to go the direction the panda is facing forward at. i.e it’s facing the opposite direction.

thanks!

Ah, I see!

Well, it may be that the panda-model isn’t facing the direction that we expected.

A minor lesson in computer-science in general, quite applicable to game-development: the sign of a given value–i.e. whether it’s a positive or a negative number–is likely to be and remain a common stumbling-block. I know that such things still catch me out on occasion!

So, try setting your beam’s y-position to -5, instead of 5.

nothing seemed to change, rlly confused too

Ah, wait, I think that I see the problem!

In your laser-generation code, you’re placing the segment from “self.source.getPos()” to “self.target.getPos()”. On the face of it, that makes sense: you’re directing it from the source to the target.

However, remember that “getPos”, without any parameters, returns a position relative to the NodePath’s parent. That means that you’re getting the position of “self.source” relative to it’s parent (which I’d guess is likely fine), and the position of “self.target” relative to it’s parent–which is (0, 5, 0) (or (0, -5, 0), depending on which sign you’re using right now).

Instead, for this code I think that you want “getPos(render)”–i.e. “get the position of the NodePath relative to render”.

That partially fixed it, but now it’s just a square. Do I have to mess with any values or anything?

Screen Shot 2019-12-13 at 10.40.55 PM

Likely! A value of 5 may be too low, especially if you have scaling applied to your panda-model. Mess with the values and find out!

had to change it to -1000 but it worked! You have no idea how much I appreciate your help :pray: :+1: :clap:

1 Like