Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / sprite physics - trapping a sprite in a circle

Author
Message
Naphier
13
Years of Service
User Offline
Joined: 2nd Oct 2010
Location: St Petersburg, Florida
Posted: 17th May 2014 21:28
I was hoping that once a sprite is within the bounds of another, larger sprite, that I'd be able to set the collide/category bits to trap it inside. Unfortunately that doesn't seem to work (very possible I have no clue what I'm doing).
Does anyone have suggestions on this? I plan to have multiple rings in my game that smaller sprites can be knocked into and need to get trapped in there. What's the best way to do this? Do I need to set up multiple sprites to surround each ring?

Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 18th May 2014 11:48 Edited at: 18th May 2014 11:51
yes, maybe build a circle wall with ~ 10 rectangles.
and SetSpritePhysicsIsBullet ( iSpriteIndex, bullet ) for inside.
alternate inside radius formula.
or hold inside with physics joints.

AGK 108 (B)19 : Windows 8.1 Pro 64 Bit : AMD Radeon HD 6670
Van B
Moderator
21
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 18th May 2014 13:34
I would clear out the circles shape, then make it with code by adding polygons in a circle, maybe leaving a 1 degree gap, but going around the other way as well, so it's like a cell.

I am the one who knocks...
Naphier
13
Years of Service
User Offline
Joined: 2nd Oct 2010
Location: St Petersburg, Florida
Posted: 18th May 2014 20:18
I've thought about surrounding them with rectangles. In my mind it is the easiest to calculate. I've only got 4 of these circles so CCD is an OK option and the dots that get pushed into the circles combine into each other on impact so there wouldn't be many of those either.

I also like the idea of using a polygon to encompass the circle's edge. I'm less sure of how to calculate that procedurally though
The rings are sized by a calculation based on the total number of dots generated so that they will be just a little bigger than the combined size of all dots for that color.

Both are great suggestions. Thank you!

Hockeykid
DBPro Tool Maker
16
Years of Service
User Offline
Joined: 26th Sep 2007
Location:
Posted: 18th May 2014 20:56 Edited at: 18th May 2014 20:56
In one of my games, Bubble Buddy I was able to get other bubbles to absorb bubble buddy by using SetCollideBits( )

Is this the type of thing you're going for?





Sean

Attachments

Login to view attachments
Naphier
13
Years of Service
User Offline
Joined: 2nd Oct 2010
Location: St Petersburg, Florida
Posted: 18th May 2014 21:00 Edited at: 18th May 2014 21:01
I'm not sure. I tried using collide bits, but when the dot is inside of the ring the dot just gets bumped out. So I need to be able to trap the dot inside of the ring. The dots themselves do "absorb" into each other to make bigger dots. That's not a problem.

Here's a shot of the prototype


Attachments

Login to view attachments
Hockeykid
DBPro Tool Maker
16
Years of Service
User Offline
Joined: 26th Sep 2007
Location:
Posted: 18th May 2014 21:06
Quote: " I tried using collide bits, but when the dot is inside of the ring the dot just gets bumped out."


Collide bits can be tricky sometimes. If I recall correctly, I believe in Bubble Buddy I turned off all of his collide bits when being absorbed into another bubble. If you posted some of the code, I might be able to help more.


Sean

Naphier
13
Years of Service
User Offline
Joined: 2nd Oct 2010
Location: St Petersburg, Florida
Posted: 18th May 2014 21:19 Edited at: 18th May 2014 21:22
It's all pretty convoluted right now, so here's the entire project. It's short and basic so it shouldn't be too difficult to figure out what I'm doing.

A couple of notes:
I initially set physics on the rings then call a couple of sync calls so that none of the rings or dots are overlapping. Then I call ClearRingPhysics() so that the rings can once again be entered. I do plan on using collide bits more because the rings should reject dots that aren't the right color.

I'm right in the middle of adding code to make a polygon around the ring, so you'll see a bit of unused code in there.

Let me know what I'm doing wrong! This is my first foray into physics, really.
Thanks!

EDIT: Also I don't really get the difference between Category bit and Collide bit. Do they both have to be used?

Attachments

Login to view attachments
Hockeykid
DBPro Tool Maker
16
Years of Service
User Offline
Joined: 26th Sep 2007
Location:
Posted: 18th May 2014 22:09 Edited at: 18th May 2014 22:12
Comment out your call to "ClearRingPhysics( )" and use this function for the HandleDots( ). I changed the GetSpriteCollision( ) to GetPhysicsCollision( ) that way collide bits will work. I've also added some code toward the bottom of the function to turn off the collision for a dot when it hits a ring. It needs a bit more logic to get it looking good, but it works.

Also, you might notice that the cursor to dots collision still uses "GetSpriteCollision()". This is because your cursor object really isn't a true physics object. When setting it up you set it up as a static physics object, it needs to be dynamic since it's moving around. Also, you seem to be positioning it using "SetSpritePosition( )", using the regular sprite commands on physics objects can completely throw off the physics system and it usually won't correctly report physics collisions (if you tried replacing it with "GetPhysicsCollision( )" the collision between the cursor and dots would never be detected).



EDIT: Some of the code indention got messed up a bit in the code snippet.

Sean

swissolo
14
Years of Service
User Offline
Joined: 9th Jan 2010
Location:
Posted: 18th May 2014 22:22 Edited at: 18th May 2014 22:23
I ran into this same problem when trying to create shield for my ships. The most important thing to remember is to remove sprites from category 0. I may be wrong, but I think 2 sprites collide if ANY of their groups are set to collide. (it certainly isn't the inverse, where they wouldn't collide if any of the groups are set not to). The category bit sets sprites to be in a given category and the collide bit sets the collision behavior between the sprite and others in given categories. So essentially the category bit determines how other sprites can collide with the given one and the collide bit sets how the selected sprite acts with other categories. So say you have a sprite I'll refer to as A. A is in category 1 and 2. Another sprite, B, is in the default category 0. If A's collide bit is set to collide with category 0, they will collide OR if B's collide bit is set to collide with category 1 or 2 they will collide. That's at least my understanding of how it works. I could have it wrong, but there isn't exactly much documentation. You mostly have to play around with it until you get the right result. All of that being said, I don't know if the bits would help you make sprites collide with the inside of another sprite I think that's the result you want, right? Creating a ring would work, in fact I've gotten that exact effect with my soft body code I posted somewhere, but I think it would be easier to just apply a force towards the center of you bubbles so the collected dots stick. If you set the dots to not collide with the ring and continually apply a force towards the center of the ring the effect would be close. Perhaps that's not what you're going for, but it would be a lot simpler.

Naphier
13
Years of Service
User Offline
Joined: 2nd Oct 2010
Location: St Petersburg, Florida
Posted: 18th May 2014 22:25
Thanks, that's not quite working though. Now a dot can initially hit and bounce off of a ring, but then it doesn't get trapped inside the ring and it can just continue off screen. And this also allows the rings to be affected by the cursor.
Looks like I have a bit of figuring out to do with the bits.

Thanks swissolo, that helps.

swissolo
14
Years of Service
User Offline
Joined: 9th Jan 2010
Location:
Posted: 18th May 2014 22:30 Edited at: 18th May 2014 22:31
Oh here's that soft body code. Not sure if that helps. I haven't looked at your code to see if you've already completed that much or not, but if you made the joints all rigid form my code (remove the center sprite, the joints to it, and restrict the outer joints) you would only have to do a tiny bit of work with category/collide bits. I'm not really sure how a dot ends up inside of a ring in your program, but you would just have to switch collide bit 1 off for a given dot (I suppose all rings would be in category 1 only in that case) when you wanted it to enter a ring and turn it back on when the dot is inside.

Hockeykid
DBPro Tool Maker
16
Years of Service
User Offline
Joined: 26th Sep 2007
Location:
Posted: 18th May 2014 23:17
Quote: "Now a dot can initially hit and bounce off of a ring, but then it doesn't get trapped inside the ring and it can just continue off screen. "


Once the collision is detected and the collide bits are set to 0, give the dot a velocity to be pulled into the center of the ring.


Sean

swissolo
14
Years of Service
User Offline
Joined: 9th Jan 2010
Location:
Posted: 18th May 2014 23:29
Hold on, if the rings are all circles why not just set them initially to never collide and use the distance between dots and the center of the ring to decide whether to enable collision or not? If dots are allowed to collide for 1 frame your results will probably look a bit strange since they'll have to stop before entering.

Naphier
13
Years of Service
User Offline
Joined: 2nd Oct 2010
Location: St Petersburg, Florida
Posted: 19th May 2014 00:02 Edited at: 19th May 2014 00:09
@swissolo - yes, the dots should not collide initially with the ring of the same color. Collision just needs to be detected, but nothing should happen until they are inside of the ring. Then they should be trapped.



So this is getting a bit complex. The velocity on the dots to pull them toward the center might be a good option.

First when I set up the objects I allow them to collide for a few frames so that none are overlapping. This prevents the dots from starting inside of any of the rings. This works well.

Then I want to set up the rings so that they repel all dots that aren't the same color, but allow dots of the same color to enter the ring. I can't wrap my head around the collide and category bits to do this. On top of that, what seems to be happening is that even if I set the rings to by static, the dots that collide with the rings move the rings.

I'm still not clear on the usage of
SetSpriteGroup (do I even need this?)
SetSpriteCategoryBits
SetSpriteCollideBits

I'm trying to have 2 Groups
Rings
Dots

I want the rings to collide with all dots that are not the same color and trap dots of the same color within the ring once they are inside of it. They should not collide with the pointer. They should not move when any collision happens. Right now the rings are moving when they collide with dots even though I've changed them from a dynamic body (I initialize them that way so they can prevent overlapping) to a static body. Why are they still reacting like a dynamic body after setting them to static with SetPhysicsOn(ring , 1)? Is it the use of category/collide bits? I guess I could just set the mass of the rings to something ridiculously high... EDIT - tried adding a ton of mass, still can push the rings... EDIT 2 - Had to call SetSpritePhysicsDelete... not sure why you can't just change the darned physics type.

I feel like once I get the groups/categories/collide bits right then I can actually determine the best method for trapping the dots inside of the rings.

First is MakeDots


Next is MakeRings with initial physics to allow all collision to prevent initial overlapping



Then after a couple of frames I update the rings physics properties



I'm failing at getting the bits right for the categories and collisions.

swissolo
14
Years of Service
User Offline
Joined: 9th Jan 2010
Location:
Posted: 19th May 2014 00:44 Edited at: 19th May 2014 03:26
Looks like setSpritePhysicsOff() just makes physics dormant and Delete actually clears it. Interesting. You won't need setSpriteGroup(). Every time I think it'll be useful I end up using the other commands . I usually use the singular version of the 2 commands we've been talking about since they make more sense to me (setSpriteCategoryBit() & setSpriteCollideBit()). Anyways, sounds like you need a category for each color ring. So say red is bit 2, green is 3, etc you would have to start off by setting all of the dots to collide with the groups that don't match their color, and to not collide with their own. (also make sure rings don't collide with category 1)
so um...
Oops I should have looked at the documentation, looks like all sprites start in category 1 not 0, my mistake.

//Red Dot (remains in category 1 only)
setSpriteCollideBit(spr, 2, 0) //don't collide with red (toggle this to true when it enters the ring)
setSpriteCollideBit(spr, 3, 1) //collide with green
//etc

//Red Ring (all of its parts if it's broken up)
setSpriteCategoryBit(spr, 1, 0) //remove from 1
setSpriteCategoryBit(spr, 2, 1) //set to be in category 2
setSpriteCollideBit(spr, 1, 0) //don't collide with all of category 1 (I think this has to be done)

something to that effect. Hopefully this isn't full of mistakes I'd hate to mislead you.

Naphier
13
Years of Service
User Offline
Joined: 2nd Oct 2010
Location: St Petersburg, Florida
Posted: 19th May 2014 01:11
Looks simpler, but I can't get it to work right
I've got:
Dots:



rings:


It's not making any sense why this doesn't work...

Naphier
13
Years of Service
User Offline
Joined: 2nd Oct 2010
Location: St Petersburg, Florida
Posted: 19th May 2014 01:23
Got it!
Needed to have the Category bits set on the dots for the rings...

Now to trap these buggers!

Naphier
13
Years of Service
User Offline
Joined: 2nd Oct 2010
Location: St Petersburg, Florida
Posted: 19th May 2014 01:51
Awesome. I've got this working pretty well now.
Thank to all of you for your help!

The dots can now only enter the same color ring and once inside they are "pulled" toward the center with velocity. I sort of wish they could freely bounce around inside of the rings, but that'll take some more coding, like trying to figure out how to plot a polygon "cell" around the ring. Might be too much maths for my brain ATM.

If anyone has an algorithm for it they'd like to share I'd be very grateful!

swissolo
14
Years of Service
User Offline
Joined: 9th Jan 2010
Location:
Posted: 19th May 2014 02:35 Edited at: 19th May 2014 02:37
Quote: "Needed to have the Category bits set on the dots for the rings..."

What do you mean?

Also if you want the physics object to be the cell (as opposed to a bunch of sprites) there's this issue
Quote: "The polygon is defined by a set of points (between 2 and 12)"

Seems like you only get 12 points. That's probably not the route to go. Although to be honest, I don't know the difference between addSpriteShapePolygon() and setSpriteShapePolygon(). Sure the documentation says set clears the old shapes, but I don't see how you would add multiple polygons with the other command as there isn't a way to differentiate between them. I guess you can have a box and a polygon that way? So sounds like the best bet is to have a circle of circles in you shape. I'm not sure how efficient that is, but I'm curious so I mind as well just try it

Naphier
13
Years of Service
User Offline
Joined: 2nd Oct 2010
Location: St Petersburg, Florida
Posted: 19th May 2014 02:47


The dots also need to belong to the same category as the ring of the same color. Otherwise the collide bit doesn't mean anything I'm guessing.

The AddSpriteShapePolygon will add an additional polygon to the sprite shape. Good for overlapping polygons for oddly shaped sprites, I guess. What you really want is SetSpriteShapePolygon which allows you to define each point.

Still, yeah I'm not sure that 12 points would be enough that would only give me 6 for the inside and 6 for the outside. The velocity trick seems pretty good. I do wish there was something more like magnets though that I could make the dots attract to the center because stting the velocity to the center isn't great. It slows down the faster moving sprites and alters their direction a bit too. Now that I've got the collision stuff figured out I'll think of something.

swissolo
14
Years of Service
User Offline
Joined: 9th Jan 2010
Location:
Posted: 19th May 2014 03:08

That's a bit more than you need, but I did it with circles.

Quote: "The dots also need to belong to the same category as the ring of the same color. Otherwise the collide bit doesn't mean anything I'm guessing"

Hmm doesn't that mean green dots won't collide with green dots?

Quote: "The AddSpriteShapePolygon will add an additional polygon to the sprite shape."

But how do you know which points are being set with the index? I assume you can only add 1 polygon because otherwise they couldn't be set apart. (unless AppGameKit waits for all the points to be set or something)

Oh and you could apply a force towards the center instead of altering their velocity.

Naphier
13
Years of Service
User Offline
Joined: 2nd Oct 2010
Location: St Petersburg, Florida
Posted: 19th May 2014 04:31
Quote: "Hmm doesn't that mean green dots won't collide with green dots?"

Nope, because all of the dots are in category 1

Quote: "But how do you know which points are being set with the index?"

Each index refers to a point. I believe they need to be made in a clockwise fashion. I can't remember, it's been a long time since I dealt with them.

Just tried out using force toward the center of the ring. It's pretty cool looking. It also seems difficult to achieve "escape velocity" which is great. Though the dots continue to wobble around the center. Fine tuning at this point. Pretty neat though!

swissolo
14
Years of Service
User Offline
Joined: 9th Jan 2010
Location:
Posted: 19th May 2014 06:05 Edited at: 19th May 2014 06:06
Quote: "Each index refers to a point. I believe they need to be made in a clockwise fashion."

Ah yes I suppose I miss-worded it. I meant which points on which polygon. If you were to add more than one, I'm not sure how you state which polygon's points are being modified.

Quote: "Nope, because all of the dots are in category 1"

Oops haha of course.

Quote: "Just tried out using force toward the center of the ring. It's pretty cool looking. "



Naphier
13
Years of Service
User Offline
Joined: 2nd Oct 2010
Location: St Petersburg, Florida
Posted: 19th May 2014 07:49
I see what you're thinking. I think...
SetSpriteShapePolygon means you're going to define the points of your own polygon. If you wanted more polygons added to the shape I'm not sure if you can use SetSpriteShapePolygon again or if you'd have to use AddSpriteShapePolygon which makes a standard polygon. I think when you use SetSpriteShapePolygon you only get one. Not sure on that though since I've not tried.
At any rate, for me, I think the force will work just fine. I've noticed that when the rings are small the dots can still be forced out, but that can be part of the challenge

swissolo
14
Years of Service
User Offline
Joined: 9th Jan 2010
Location:
Posted: 20th May 2014 22:23
I know you're happy with your results so you don't really need this, but I think this is what you were looking for originally.

Much better cage In case anyone comes looking later on.

Naphier
13
Years of Service
User Offline
Joined: 2nd Oct 2010
Location: St Petersburg, Florida
Posted: 20th May 2014 22:29
I might give this a shot. The updateCharacter function looks especially helpful. Thanks!

swissolo
14
Years of Service
User Offline
Joined: 9th Jan 2010
Location:
Posted: 20th May 2014 22:56
Quote: "The updateCharacter function looks especially helpful"

Oh really? That code was thrown together really quickly haha I just needed some way to move the ring with user input. Here's a slightly cleaner version

Not much different but you can tell more of what's going on.

BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 21st May 2014 14:43
A couple of pointers.

Collision bits - http://www.thegamecreators.com/pages/newsletters/newsletter_issue_131.html#6

Regarding VanB's point way up there at the beginning. What he is saying is that if you create a collision polygon in reverse (anti-clockwise) then it will hold your dots inside the shape. Polygons are created clockwise to create a "normal" collision model.

Naphier
13
Years of Service
User Offline
Joined: 2nd Oct 2010
Location: St Petersburg, Florida
Posted: 21st May 2014 18:53

Login to post a reply

Server time is: 2024-04-28 12:18:04
Your offset time is: 2024-04-28 12:18:04