2D

Can panda3d do 2D graphics? If you can, could you please give me an example of how to layout sprites?

It’s quite possible, I do believe.

As to your second question, what do you mean when you refer there to “laying out” sprites? Are you asking how to display them, how to animate them, how to arrange them on a sprite sheet, or something else?

If you’re asking about arranging your sprite-sheet images, then I think that this depends somewhat on how you go about implementing your sprites.

For an example of a simple 2D game, perhaps take a look at the Asteroids sample program.

I need to know how to display the sprites.

You will want a square mesh. Assign your sprite image to the mesh. Parent the mesh to render. This will put it in the 3d scene.

There are two ways to make it “2d”. You can use the 2d Ui layer (aspect2d or pixle2d). You can create your own 2d layer by reading about orthographic lenses in the panda manual.

That’s the core of it, I do believe: place your image on a rectangular mesh, then–depending on your desired outcome–parent it to one of render, aspect2d or pixel2d, with appropriate scaling and positioning.

You may want to look at the manual page on Automatic Texture Animation, which might be a way of producing animated sprites. However, this may be somewhat limiting if you want to use sprite sheets (I don’t think that it’s impossible, but it would likely be difficult at the least); as a result, you may want to consider using SequenceNode to create your own sprite-animation class. Depending on your intentions, you may end up creating some sort of sprite class that incorporates these sprite-animations and whatever logic is appropriate to your game.

It’s also quite possible that there’s a sprite class already available in the Code Snippets sub-forum–it might be worth taking a look there.

(I don’t think that we have a dedicated “sprite” class (outside of that used by the particle renderer), but I may be mistaken.)

Using texture atlases for animations ain’t that hard. I had a demo using them but the link is dead. You can use this code:
github.com/wezu/Avolition/blob/master/vfx.py

Or wait for me to reupload a working demo to:
github.com/wezu/vfx

I got it working, but I can’t figure out collisions. Can someone help me figure out collisions? I am new to panda.

Are you using one of the physics engines?

I am not sure. what physics engines does panda3d have?

To the best of my knowledge, Panda has both its own, internal physics engine–this isn’t the most powerful option, but is fairly simple to set up–as well as bindings for a few other middleware engines: offhand, I’m aware of Bullet being available, and think that ODE and PhysX might be (I haven’t really looked into those latter two myself).

What sort of physics do you want to have in your game? Is it just a matter of moving and jumping along platforms, with perhaps some simple shooting, or do you want a more complex simulation?

This is the game I am trying to remake https://www.youtube.com/watch?v=wj2XV6c-0yI. Right now most of the code is from asteroids.

Heh, I remember that game. :slight_smile:

Hmm… PInball physics might be best served with a physics engine that’s fairly rigorous. I’m not sure that the built-in physics engine is up to this sort of physics, the flippers in particular–although I do stand to be corrected. Bullet should, I think, be up to the job; I don’t know how the other middleware engines compare.

For a start on working with Bullet, take a look at the manual, starting here, I believe. The “Bullet Hello World” in particular should introduce you to the basics of Bullet.

Using bullet should work well. It will help if you have had a physics class so you know how to think about physics problems. You shouldn’t need to do any math yourself.

If you are sticking with 2d you will want to make sure your physics code is separate from your graphics code. You will want your physics code to work in 3d (because your pinball machine has a slope and ramps).

Working in bullet. here is my progress.

Having some troubles getting the bouncing ball to work. Thanks ahead if you can help. Here is the source code.

import sys
from panda3d.bullet import BulletPlaneShape
from panda3d.bullet import BulletRigidBodyNode
import direct.directbase.DirectStart
from direct.gui.OnscreenImage import OnscreenImage
from direct.showbase.DirectObject import DirectObject
from direct.showbase.InputStateGlobal import inputState
from panda3d.bullet import BulletBoxShape
from panda3d.core import AmbientLight
from panda3d.core import DirectionalLight
from panda3d.core import Vec3
from panda3d.core import Vec4
from panda3d.core import Point3
from panda3d.core import TransformState
from panda3d.core import BitMask32
from panda3d.bullet import BulletSphereShape

from panda3d.bullet import BulletWorld
from panda3d.bullet import BulletPlaneShape
from panda3d.bullet import BulletBoxShape
from panda3d.bullet import BulletRigidBodyNode
from panda3d.bullet import BulletDebugNode
from panda3d.bullet import BulletTriangleMesh
from panda3d.bullet import BulletTriangleMeshShape
class Game(DirectObject):
  #imageObject = OnscreenImage(image = 'textures/Board.png', pos = (-0.0, 0, 0.0),  scale = (1.5, 1.5, 1.5))
  def __init__(self):
   # base.setBackgroundColor(0.1, 0.1, 0.8, 1)
    base.setFrameRateMeter(True) 

    base.cam.setPos(0, -20, 4)
    base.cam.lookAt(0, 0, 0)
 
    base.disableMouse() 
	 # Light
    alight = AmbientLight('ambientLight')
    alight.setColor(Vec4(0.5, 0.5, 0.5, 1))
    alightNP = render.attachNewNode(alight)

    dlight = DirectionalLight('directionalLight')
    dlight.setDirection(Vec3(1, 1, -1))
    dlight.setColor(Vec4(0.7, 0.7, 0.7, 1))
    dlightNP = render.attachNewNode(dlight)

    render.clearLight()
    render.setLight(alightNP)
    render.setLight(dlightNP)

    # Input
    self.accept('escape', self.doExit)
    

    inputState.watchWithModifiers('forward', 'w')
    inputState.watchWithModifiers('left', 'a')
    inputState.watchWithModifiers('reverse', 's')
    inputState.watchWithModifiers('right', 'd')
    inputState.watchWithModifiers('turnLeft', 'q')
    inputState.watchWithModifiers('turnRight', 'e')

    # Task
    taskMgr.add(self.update, 'updateWorld')

    self.setup()

  # _____HANDLER_____

  def doExit(self):
    self.cleanup()
    sys.exit(1)

  def doReset(self):
    self.cleanup()
    self.setup()

  def toggleWireframe(self):
    base.toggleWireframe()

  def toggleTexture(self):
    base.toggleTexture()

  def toggleDebug(self):
    if self.debugNP.isHidden():
      self.debugNP.show()
    else:
      self.debugNP.hide()

  def doScreenshot(self):
    base.screenshot('Bullet')
  # ____TASK___
  def processInput(self, dt):
    force = Vec3(0, 0, 0)
    torque = Vec3(0, 0, 0)

    if inputState.isSet('forward'): force.setY( 1.0)
    if inputState.isSet('reverse'): force.setY(-1.0)
    if inputState.isSet('left'):    force.setX(-1.0)
    if inputState.isSet('right'):   force.setX( 1.0)
    if inputState.isSet('turnLeft'):  torque.setZ( 1.0)
    if inputState.isSet('turnRight'): torque.setZ(-1.0)

    force *= 30.0
    torque *= 10.0

    

  def update(self, task):
    dt = globalClock.getDt()

    self.processInput(dt)
    #self.world.doPhysics(dt)
    self.world.doPhysics(dt, 5, 1.0/180.0)

    return task.cont

  def cleanup(self):
    self.world.removeRigidBody(self.groundNP.node())
    self.world.removeRigidBody(self.boxNP.node())
    self.world = None

    self.debugNP = None
    self.groundNP = None
    self.boxNP = None

    self.worldNP.removeNode()

  def setup(self):
    self.worldNP = render.attachNewNode('World')

    # World
    self.debugNP = self.worldNP.attachNewNode(BulletDebugNode('Debug'))
    self.debugNP.show()
    self.debugNP.node().showWireframe(False)
    self.debugNP.node().showConstraints(True)
    self.debugNP.node().showBoundingBoxes(False)
    self.debugNP.node().showNormals(True)
    #self.debugNP.showTightBounds()
    #self.debugNP.showBounds()
    self.world = BulletWorld()
    self.world.setGravity(Vec3(0, 0, -6.81))
    self.world.setDebugNode(self.debugNP.node())
    shape = BulletPlaneShape(Vec3(0, 0, 1), 1)

    self.groundNP = self.worldNP.attachNewNode(BulletRigidBodyNode('Ground'))
    self.groundNP.node().addShape(shape)
    self.groundNP.setPos(0, 0, -7)
    self.groundNP.setCollideMask(BitMask32.allOn())

    self.world.attachRigidBody(self.groundNP.node())
      # Box (dynamic)
    shape = BulletBoxShape(Vec3(0.5, 0.5, 0.5))

    self.boxNP = self.worldNP.attachNewNode(BulletRigidBodyNode('Box'))
    self.boxNP.node().setMass(1.0)
    self.boxNP.node().addShape(shape)
    self.boxNP.setPythonTag("velocity", 1000000000000.001)
    

    self.boxNP.setPos(0, 0, 2)
    self.boxNP.setScale(0.5, 0.5, 0.5)
    self.boxNP.setCollideMask(BitMask32.allOn())
    #self.boxNP.node().setDeactivationEnabled(False)

    self.world.attachRigidBody(self.boxNP.node())

    visualNP = loader.loadModel('models/ball')
    visualNP.clearModelNodes()
    visualNP.reparentTo(self.boxNP)
    shape = BulletSphereShape(0.6)

    # Bowl
    shape = BulletBoxShape(Vec3(0.5, 0.5, 0.5))
    self.boxNP = self.worldNP.attachNewNode(BulletRigidBodyNode('Boxx'))
    self.boxNP.node().setMass(0.0)
    self.boxNP.node().addShape(shape)
    self.boxNP.setPos(0, 0, -1.5)
    self.boxNP.setScale(1.5, 0.5, 0.5)
    self.boxNP.setCollideMask(BitMask32.allOn())
    #self.boxNP.node().setDeactivationEnabled(False)
    self.world.attachRigidBody(self.boxNP.node())
    tex = loader.loadTexture('textures/Flipper1.png')
    visualNP = loader.loadModel('models/plane')

    visualNP.clearModelNodes()
    visualNP.reparentTo(self.boxNP)
    visualNP.setTexture(tex, 1)

'''
    np = self.worldNP.attachNewNode(BulletRigidBodyNode('Sphere'))
    np.node().setMass(1.0)
    np.node().addShape(shape)
    np.setPos(-4, 0, 4)
    np.setCollideMask(BitMask32.allOn())
    self.world.attachRigidBody(np.node())
'''
fgame = Game()
run()

… What problem are you having? What have you tried? Are there any errors?

(I haven’t read through all of that code.)

the ball won’t roll down. here is an image. http://s23.postimg.org/70uecos23/Screen_Recording_2_1.gif

Is the ground sloped?

How do i make the ground sloped?

You should be able to do this by simply rotating it, I believe, (as described here).