I was wondering how to set positions for a collision box for code so if you walk between the position you put it will trigger and you could put code when you walk into the box it will lets say take you back to spawn. Please Help
Your question is a little unclear to me, Iām afraid. Iāll answer as best I understand it.
First of all, which collision system are you using? For the moment Iām going to presume that youāre using Pandaās built-in collision system; if not, some of the below might not apply.
You set the position of a collision object in the same manner as any other NodePath, I believe: āmyNodePath.setPos(position)ā.
In essence, you would seem to want a collision object with an event registered to it, handled via a CollisionHandlerEvent object.
If youāre already using a CollisionHandlerPusher (or one of its descendants) to prevent objects from intersecting, you can use this in place of a separate CollisionHandlerEvent (CollisionHandlerPusher inherits from CollisionHandlerEvent), I believe. In this case, presuming that you want the trigger to be ānon-solidā (that is, you donāt want objects to stop when they hit it), you would additionally call the āsetTangibleā method of the solid, passing in āFalseā to indicate that it be intangible.
That done, you add an appropriate event-pattern to your CollisionHandlerEvent/CollisionHandlerPusherāthat is, register one or more of āinā, āoutā, and āagainā patternsāand have your Direct/ShowBase object āacceptā your events, allowing you to link them to methods of your choosing. (See the link attached to the word āCollisionHandlerEventā, above.)
I believe that the manual gives a fuller description, including examples, starting here.
How you use Pandaās built-in collision system can you please give me a code or something because right now iām a little confused. Like put it all in code.
There are two examples in the manual that do exactly that, I believeāone for CollisionHandlerPusher, and one for CollisionHandlerEvent. o_0
Otherwise, what, specifically, is confusing you? Perhaps I can help.
I Need CollisionHandlerEvent because the CollisionHandlerPusher is like if you donāt want to run into walls so if you go to a wall it will not let you go through the model. If I use CollisionHandlerEvent it will allow me to go through
the model and active it. I look at the code and I donāt get it I read what it says for CollisionHandlerEvent I kinda get it hereās the code:
class MyObject(DirectObject.DirectObject):
def __init__(self):
self.accept('car-into-rail', handleRailCollision)
def handleRailCollision(self, entry):
print entry
So do I take it then that you wonāt have walls or obstacles that the object should be stopped by, or that youāre handling that sort of collision in some way other than CollisionHandlerPusher? If so, then fair enough; if not, then Iāll point out that CollisionHandlerPusher can produce events just as a CollisionHandlerEvent can, and collision objects can be made āintangibleā, and thus produce events without preventing objects from moving through them, I believe.
Okay, fair enough. Perhaps it would help to go through the process one step at a time.
The code that you posted looks like a decent start: you have a descendant of DirectObject that accepts an event, and a method which that event should call. There is one issue that I see: in the call to āacceptā, the method that youāre telling it to call appears to be declared in the same class (āhandleRailCollisionā), but you arenāt specifying this when you refer to the method in your call to āacceptā: in short, youāre missing āself.ā before āhandleRailCollisionā, I believe.
Moving on from there, letās start by finding out where youāre getting lost:
- Do you know how to create collision solids, and add them to your scene?
- Do you know how to create a traverser?
- Do you know how to create a collision handler?
- Do you know how to tell the handler and traverser about your objects?
I was wondering thereās a way like this /
if localAvatar.getY() < place and localAvatar.getX() < place1 and localAvatar.getX() > place2 and localAvatar.getX() > place3
so the places I can make like a box with the places with x & y I was wondering if I can do that?
Depending on what a āplaceā is, yes, that seems reasonable.
Youād presumably define your own regions (that is, you have some class or tuple storing this information), each with a position (or a NodePath defining a position) and a size. You then compare the position of your ālocalAvatarā to the position of the region, plus- and minus- its size.
If Iām not much mistaken, note that a point is within a rectangle when the pointās x-value lies between the rectangleās left- and right- edges, and the pointās y-value lies between the rectangleās bottom- and top- edges. The left edge of a rectangle can be defined as the x-value of rectangleās centre-point minus half of the rectangleās width, and its right edge can be defined as the x-value of the rectangleās centre-point plus half of the rectangleās width. Much the same can be done for the top- and bottom- edges of the rectangle, but using the y-value of its centre-point and its length.
In short, something like this:
# I'll store a few values in local variables so that the "if-statement" doesn't get too messy...
avatarX = localAvatar.getX()
avatarY = localAvatar.getX()
regionX = someRegion.nodePath.getX()
regionY = someRegion.nodePath.getY()
halfRegionWidth = someRegion.width/2.0
halfRegionLength = someRegion.length/2.0
if avatarX > regionX - halfRegionWidth and avatarX < regionX + halfRegionWidth and avatarY > regionY - halfRegionLength and avatarY < regionY + halfRegionLength:
# Do something...
for
is it the position because the problem there is itās a full town so how would I do it
like the model is all in one model so theres no like multiple models to make the town.
The āregionsā (of which āsomeRegionā would be just one) are things that you define, most likely a class of your own creation. There are a number of means by which you could specify their positions, depending on what works best for you and for your projectāoffhand, three approaches come to mind (although there may be more):
- You could use place-holder objects in your level-model, which you search for and replace when the level is loaded
- You could have a separate text-file describing their locations, hand-written by you
- You could put together some sort of level-editor in which you place them, and which allows you to save them out to a file
Let me explain why iām here in the first place. I was on youtube, and I would type like Panda3d tutorials Toontown I like Toontown and stuff then thereās this guy who did like a collision, but heās not there anymore when I type his name and still on there on YouTube and iām trying to do it his way I remember some of his code so this is what I got so far, but doesnāt seem to work.
if localAvatar.getX() > 22 and localAvatar.getY() > -149 and localAvatar.getX() < 24 and localAvatar.getY() < -151:
toontowncentral.detachNode()
loader.unloadModel('toontown.bam')
Quesion: is x the first or second, and is y the first or second?
Iām afraid that I have no real idea: Iāve never played Toontown, nor taken part in one of the attempts that I believe that Iāve seen to re-create it.
[edit] But why is it important to use the approach that this other person did? Why not develop something that works for you?
True true but that was the easiest way for me to understand.
Here is what I used in an old project
def updateTunnel(task): #done
global loadedArea
global toontowncentral
global dg
x = Actor.getPos().get_x()
y = Actor.getPos().get_y()
#Toontown Central to DG
if x >= 23.8046 and x <= 37.7877 and y <= -149.988 and y >= -158.263 and loadedArea == 'toontowncentral':
toontowncentral.remove()
toontowncentral = loader.unloadModel('phase_4/models/neighborhoods/toontown_central_full.bam')
dg = loader.loadModel('daisys_garden_sz.bam')
dg.reparentTo(render)
Actor.setPosHpr(72.0447, 183.742, 10.208, -209.71, 0, 0)
loadedArea = 'dg'
#DG To Toontown Central
if x >= 74.2401 and x <= 83.6807 and y >= 180.808 and y <= 194.032 and loadedArea == 'dg':
dg.remove()
dg = loader.unloadModel('street_daisys_garden_sz.bam')
loadedArea = 'toontowncentral'
toontowncentral = loader.loadModel('phase_4/models/neighborhoods/toontown_central_full.bam')
toontowncentral.reparentTo(render)
Actor.setPosHpr(34.8669, -147.037, 2.525, 687.063, 0, 0)
return Task.cont
base.taskMgr.add(updateTunnel, 'UpdateTunnel')
Hope this helps