Hello all! I’ve got a human model with a gun in hand and I would like to know if it’s possible to make bullets (which can be represented by any bullet-like model) being fired from the gun? If I have the bullet model, can I make it move from the gun to the target? Is any piece of code available,please?
hi and welcome to panda,
for your questions.short answer. yes its possible.
small addition. bullets move so fast, you wont see them unless your game has bullet-time.
for the “how-to”, its as simple as loading a model and move it forwards. you can do that with a task or a lerp-interval. both things are covered in the manual and you need both things to move your player.
those belong to the very basics of object manipulation so once you know a little more about game-programming it’s really easy to do.
So, I just need to load the bullet and then reparent it to the gun? Btw, is there going to be any clash if I load a model with the same name everytime?
For example, if I press ‘f’ I load a model named “bullet” and then move it towards the target. If I subsequently press ‘f’ again, it will load another model with the same name… How do I cater for that part? Using “arrays” of models? Lastly, is it possible to automatically move a model towards a given target in Panda?
you dont neccessarily want to parent it to the gun, since it would follow the gun then. just position it to the gun and then move it from there.
there is no problem loading the model several times. but you have to keep track of them yourself. may it be by using node-path tags, or keeping them in lists,dictionaries, sets or whatever you like.
for your last question: its semi-automatic. you can set up a task, or a lerp interval to move it, it will go pretty much straight. you can also use lookAt to get something like a missle-homing behavior.
a general answer to your "can panda do … ". in 99% of the cases the answer is “yes, if you know how to code python” . in about 1% its “no cause your hardware isnt capable of doing it” or “no except you can code c++”
once you practiced programming a bit but those exceptions really are rare.
I’ve managed to do this till now:
def enemy(self, task): #elapsed = task.time - self.prevtime if (math.sqrt((self.enemy1.getX()-self.player.getX())**2 + (self.enemy1.getY()-self.player.getY())**2) <= 4) or (self.isShooting is True): if (self.enemy_ammo>0) and (self.enemy_bullet_loaded is False): enemy_bullet = loader.loadModel('models/bullet') enemy_bullet.reparentTo(self.enemy1) enemy_bullet.setScale(0.1) enemy_bullet.lookAt(self.player) enemy_bullet.setX(self.enemy1.getX()+28) enemy_bullet.setY(self.enemy1.getY()+4) enemy_bullet.setZ(57) self.enemy1.play("fire") # Need to enter bullet movement here self.enemy_ammo = self.enemy_ammo - 1 if (self.player.getY()>self.enemy_bullet.getY()): self.enemy_bullet_loaded = False return Task.cont
It’s for the enemy part. However, how do I make the bullet move towards the player’s model? And how do I check for collision between the ‘player’ model and the ‘enemy_bullet’ model? I’ve seen the tutorials, but I don’t understand anything…
Since it looks like you are doing the movement inside of a task (instead of using a poslerp), you really arent that far away. I think an easy first solution for getting your bullet to travel at the guy just involves a little bit of linear algebra.
So once you have your node path already looking at the node path of its destination target, most of the work is already finished! Define a speed somewhere (with respect to the size of models environ etc), and some panda calls can help you along the way.
self.speed = 100 #some number that makes sense to you, with respect to the speed of the task velocityVector = self.bulletNode.getQuat().getForward() #returns a unit vector pointing forward from your model, should already be poiting at the target since you called lookAt() velocityVector = velocityVector * speed #now a vector with magnitude of speed self.bulletNode.setPos(self.bullet, velocityVector, velocityVector, velocityVector) #this just adds your velocity onto the bullets current position
Eventually (well actually at this speed rather instantly), the bullet will go toward his target (or the position he was at when you called lookAt()).
As for collisions, there is a good manual page worth reading on them, as well as rapidly moving objects (which bullets are).
I’m stuck with the bullet. It does not move…
Can you give me the links to the manual concerning rapidly moving objects?
Secondly, I have thought of using a sort of “laser gun” instead of the bullet. Can this be done using Collision Ray? The enemy will “fire” its collision ray, while the player will do the same.
Concerning collision detection, I’m still not being able to understand it. Is there any code available that checks if there is collision between two particular objects? For example, if I have three actors, I want to check if there is collision between actor1 and actor 2 only. How do I do it?