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:

Or wait for me to reupload a working demo to:

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.cam.setPos(0, -20, 4)
    base.cam.lookAt(0, 0, 0)
	 # 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)


    # 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')


  # _____HANDLER_____

  def doExit(self):

  def doReset(self):

  def toggleWireframe(self):

  def toggleTexture(self):

  def toggleDebug(self):
    if self.debugNP.isHidden():

  def doScreenshot(self):
  # ____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.world.doPhysics(dt, 5, 1.0/180.0)

    return task.cont

  def cleanup(self):
    self.world = None

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


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

    # World
    self.debugNP = self.worldNP.attachNewNode(BulletDebugNode('Debug'))
    self.world = BulletWorld()
    self.world.setGravity(Vec3(0, 0, -6.81))
    shape = BulletPlaneShape(Vec3(0, 0, 1), 1)

    self.groundNP = self.worldNP.attachNewNode(BulletRigidBodyNode('Ground'))
    self.groundNP.setPos(0, 0, -7)

      # Box (dynamic)
    shape = BulletBoxShape(Vec3(0.5, 0.5, 0.5))

    self.boxNP = self.worldNP.attachNewNode(BulletRigidBodyNode('Box'))
    self.boxNP.setPythonTag("velocity", 1000000000000.001)

    self.boxNP.setPos(0, 0, 2)
    self.boxNP.setScale(0.5, 0.5, 0.5)


    visualNP = loader.loadModel('models/ball')
    shape = BulletSphereShape(0.6)

    # Bowl
    shape = BulletBoxShape(Vec3(0.5, 0.5, 0.5))
    self.boxNP = self.worldNP.attachNewNode(BulletRigidBodyNode('Boxx'))
    self.boxNP.setPos(0, 0, -1.5)
    self.boxNP.setScale(1.5, 0.5, 0.5)
    tex = loader.loadTexture('textures/Flipper1.png')
    visualNP = loader.loadModel('models/plane')

    visualNP.setTexture(tex, 1)

    np = self.worldNP.attachNewNode(BulletRigidBodyNode('Sphere'))
    np.setPos(-4, 0, 4)
fgame = Game()

… 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).