Creating a Basketball Net

In my game, I’m trying to create a basketball net. I’ve setup the chains like in the chains sample code, but I want to link the chains together like in an actual basketball net. Can somebody help me do this? Thanks in advance! [import]uid: 38001 topic_id: 28352 reply_id: 328352[/import]

It may help to post plug and play or a sample people could work from to try and assist you.

That said, I wonder what your goal is here - is the ball going to go through the net or stay in it? What kind of physics body are you using for your chains and what will you use for the ball?

(Just trying to picture this and hopefully be able to assist.)

Peach :slight_smile: [import]uid: 52491 topic_id: 28352 reply_id: 114582[/import]

I want the ball to go through the net like a normal basketball net. I was thinking that if the ball successfully goes into the net, then I could temporarily make the net a sensor like the rim is.This is what my net looks like so far (I changed the pictures to rectangles to make it easier for you to run it ):

 --create the net  
 local Rim = display.newRect( 0, 0, 70, 10 )  
 Rim.x = 166; Rim.y = 57  
 physics.addBody( Rim, "static", { friction=0.5 } )  
 Rim.isSensor = true  
 Rim:setFillColor( 0 )  
 group:insert( Rim )  
 local myJoints = {}  
  
 for i = 1,5 do  
 local link = {}  
 for j = 1,5 do  
 link[j] = display.newRect( 0, 0, 5, 10 )  
 link[j].x = 121 + (i\*15)  
 link[j].y = 55 + (j\*12)  
 group:insert( link[j] )  
 physics.addBody( link[j], { density=2.0, friction=0, bounce=0 } )  
  
 -- Create joints between links  
 if (j \> 1) then  
 prevLink = link[j-1] -- each link is joined with the one above it  
 else  
 prevLink = Rim -- top link is joined to overhanging beam  
 end  
 myJoints[#myJoints + 1] = physics.newJoint( "pivot", prevLink, link[j], 121 + (i\*15), 46 + (j\*12) )  
  
 end  
 end  

Also, here is my ball:

 -- create the basketball  
 ball = display.newCircle( 0, 0, 15, 15 )  
 ball.myName = "basketball"  
 physics.addBody( ball, "kinematic", {density = 2.9, radius = 15} )  

One more thing, when the ball is being shot, the body type becomes dynamic instead of kinematic. [import]uid: 38001 topic_id: 28352 reply_id: 114615[/import]

Hi there,

I haven’t run your code yet (away from my computer at the moment), but is the problem you’re having that your code works, but what it creates doesn’t look like a basketball net?

If I’m reading your code right, it looks like it will create five parallel, vertical chains hanging down from the rim, each with five links. But since the chains aren’t connected to each other, it won’t look like a basketball net. If that’s the problem, you might consider simply adding some horizontal “cross hatches” to make it look more like a net.

Your strategy of turning the net into a sensor when the ball is going trough it is interesting. Here’s something to keep in mind (maybe you’re thinking of it already): if the ball hits the net from below, the net will move due to the collision (which will be a nice graphical effect). But when the ball goes through the net from above, the net will be still since you’ll have turned it temporarily into a sensor (so you’ll be missing the graphical effect).

  • Andrew [import]uid: 109711 topic_id: 28352 reply_id: 114644[/import]

Hi Andrew,

Yes that’s exactly my problem. How can I make the horizontal “cross hatches”?

And yes I thought about that too, but I can’t think of a better way to do that. Any suggestions?

  • Anish [import]uid: 38001 topic_id: 28352 reply_id: 114819[/import]

Hi there,

The “cross-hatches” would be horizontal rectangles between the vertical ones. Unfortunately I’m away from my computer (again!), so I can’t easily write out any code, but basically what you are trying to create is a grid of vertical and horizontal rectangle where every intersection is a pivot joint. For example, in your j loop, create a horizontal rectangle going to the left, and attach it to the vertical rectangle by a pivot as well as the vertical rectangle in the prior column.

As for the second issue, that’s more difficult. Perhaps you could use your strategy, but when the ball passes through the hoop from above (and you make the net a sensor), you apply some random forced to the net (or perhaps directionally relative to the ball), so that the net still appears to move in response to the ball.

  • Andrew [import]uid: 109711 topic_id: 28352 reply_id: 114904[/import]

Hi,

Ok so I’ve fixed the second issue by applying a force, like you said, which is relative to the ball’s velocity.

I’m still trying to work on the cross-hatches. Could you help me out with the code when you are at your computer later?

  • Anish [import]uid: 38001 topic_id: 28352 reply_id: 115071[/import]

Hi Anish,

I was wondering if you could post the code, in which you added a force on the net, when the ball hits it? It would be very helpful. Thanks. [import]uid: 175550 topic_id: 28352 reply_id: 135358[/import]

Hi Anish,

I was wondering if you could post the code, in which you added a force on the net, when the ball hits it? It would be very helpful. Thanks. [import]uid: 175550 topic_id: 28352 reply_id: 135358[/import]