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 / Help with magnetic sprite needed

Author
Message
lilpissywilly
AGK Developer
13
Years of Service
User Offline
Joined: 10th Sep 2010
Location: Office Chair
Posted: 22nd Feb 2012 12:38
Hello.

I've made a simple kids app where you generate simple shapes (square, circle, triangle and rectangle) by clicking on buttons.
There is no point to it just the fun of tossing around different shapes.

I'm using box2d for physics which is very nice, but I've come across a problem when trying to make one of the shapes, triangles, magnetic.

Never having used box2d before I didn't know where to begin, but I started out by checking all triangles and using "SetSpritePhysicsForce" where the vector points to a shape for the magnet to be drawn towards. I then used a weld joint to join them until the magnetsprite was clicked.

This however didn't prove very practical, it was hard making a weld joint as the shapes weren't necessarily lined up right, which causes funny jumps in the game.

My next idea is to create dummy sprites on surfaces suitable for attaching on each shape and thereby being able to line them up better. But I worry about performance with this.

I'm open to suggestions, what would be the common way of doing this with a physics engine? Any input is appreciated

If need be I can upload the app but this isn't code specific, it's more implementation of general thinking around a problem

Thanks

My hovercraft is full of eels
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 22nd Feb 2012 14:46
Which part are you having trouble with? Making sprites magnetic or making triangular shapes?

I may have some suggestions for both but if it's just one I can save some typing time

lilpissywilly
AGK Developer
13
Years of Service
User Offline
Joined: 10th Sep 2010
Location: Office Chair
Posted: 22nd Feb 2012 14:53
Sorry.

The triangle is made fine. It's just how to approach the magnetism-simulation I've struggled with

My hovercraft is full of eels
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 22nd Feb 2012 14:57 Edited at: 22nd Feb 2012 14:59
If you keep track of the "state" of each sprite in an array like this:


...then you can cycle through each magnet and cycle through the sprite array checking for sprites that are magnetic within a certain range of the magnet.



Then you can apply a force to the sprite related to its distance from the magnet and direction to it.

lilpissywilly
AGK Developer
13
Years of Service
User Offline
Joined: 10th Sep 2010
Location: Office Chair
Posted: 22nd Feb 2012 15:13
Yes that is how I tried to solve it. And that part did work. But this made them break up too easy, it was difficult putting enough force on the magnets using setspritephysicsforce. So then I added a weld joint when the magnet touched the object it pursued.

I think the problem is that setspritephysicsforce is directing the force in a vector, and I had to multiply it substantially to make it strong enough. And then the issue of weld joint where the sprites can lock together even if they're not "lined up". I.e a triangle could lock on with a corner to another shape

My hovercraft is full of eels
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 22nd Feb 2012 15:28
Quote: "the issue of weld joint where the sprites can lock together even if they're not "lined up". I.e a triangle could lock on with a corner to another shape"

Not sure what you mean here. Perhaps a small demo is a good idea?

lilpissywilly
AGK Developer
13
Years of Service
User Offline
Joined: 10th Sep 2010
Location: Office Chair
Posted: 22nd Feb 2012 16:18 Edited at: 22nd Feb 2012 19:55
I will upload a demo a bit later when I'm at the computer. I'll make another stab at explaining.
If I use a weld joint upon point of impact of two objects; they'll be stuck together with the same angle they had at impact. Which isn't preferable since a triangle can then stand on one of its three ends on another object. Which a magnet wouldn't.

I'm not familiar with how other joints would act and the documentation is quite sparse on the subject

My hovercraft is full of eels
lilpissywilly
AGK Developer
13
Years of Service
User Offline
Joined: 10th Sep 2010
Location: Office Chair
Posted: 23rd Feb 2012 13:34
Ok here is the upload.

Please only click on the triangle and the rectangle buttons at the bottom since I never finished any other part of the magnetism, this is solely so that you can see the flaw with the way I tried to make it work. You can pull around the shapes once they are dropped of course.

My hovercraft is full of eels

Attachments

Login to view attachments
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 19th Mar 2012 23:16
Out of curiosity, I downloaded and tried the game.

I think little kids will definitely have fun with. I did.

I found it amusing to throw objects back out again and they disappear if you use the right angle (so that they'd come back and miss the pipe).

Good luck with this!

Cheers,
Ancient Lady
lilpissywilly
AGK Developer
13
Years of Service
User Offline
Joined: 10th Sep 2010
Location: Office Chair
Posted: 19th Mar 2012 23:45
Why thank you! That inspires me to keep working on it

My hovercraft is full of eels

Login to post a reply

Server time is: 2024-05-07 18:32:40
Your offset time is: 2024-05-07 18:32:40