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 / Any physics examples?

Author
Message
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 11th Feb 2014 00:09 Edited at: 11th Feb 2014 01:39
I think we all could use a tutorial on how to add physics to our sprites. If there is one available, someone point me in the direction. Otherwise, I'll figure it out and write one.

I need objects to bounce off each other (and know when it happens). There is no friction nor gravity in this world (outerspace). I need some control over positions, so when they go off screen I can wrap them around from the other sides.

For the most part, it's rather easy to figure out the basics. But two things are giving me trouble; constant speed and manually setting a position.

I'm calling setSpritePhysicsForce() each iteration but that just seems to build up acceleration. So then I tried setting a sprite's friction to 0 and applying the force only once and using a restitution value of 1 but then the sprite doesn't move at all.

Ah wait, I need a much MUCH larger force applied. Now I only have to work out how to set the sprite's position so I can wrap it around the screen.

Figured it out. Setting a sprite's position stops its velocity. So I just need to record its current velocity, update the position, then reapply the velocity again.

Naphier
14
Years of Service
User Offline
Joined: 2nd Oct 2010
Location: St Petersburg, Florida
Posted: 11th Feb 2014 02:21
I found that it took a lot of fussing around the values to get things to work just right. Don't forget to adjust for frame rate by using step physics.

A good tutorial would be nice, but I can imagine it would easily become complex. Another idea would be a physics sandbox. I think there is one for particles out there. A nice one for physics would be awesome. Maybe Easter Bunny with his/her experience in using physics would make something like this? I imagine quite a few of us would buy it if done well.

Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 11th Feb 2014 03:59
Quote: "Don't forget to adjust for frame rate by using step physics."

Something for me to read up on.

Is there a better option to getPhysicsCollision? I've noticed that it doesn't always pick up on my collisions. I see the collision response happening, but I can't initiate certain actions if I can't catch when they hit.

Naphier
14
Years of Service
User Offline
Joined: 2nd Oct 2010
Location: St Petersburg, Florida
Posted: 11th Feb 2014 04:20
Paul references this example somewhere in the forums:

It works great and just pushes the physics along by the necessary frames to keep it at a desired 60fps (physicsStep# =~ 1/60).

I'm confused a little by your question about collision. Is it just missing the collision some of the time? If you need to do things before they hit then you might want to add a sprite shape that acts like a ray cast. Or a dummy sprite that can catch collisions before they happen. Sorry i can't be too much help on that. I don't use physics extensively in my code. I also don't use collision and just have an object bouncing off of the floor.

Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 11th Feb 2014 05:45 Edited at: 11th Feb 2014 05:49
For instance:


I'll play a sound if two objects collide, or a different sound if one of the objects is the player.

But, as long as I'm pressing left or right arrow key to control player's rotation:


A contact with the player sprite is never returned. The collision still happens as you can see the response on screen when the game is running, but it doesn't get picked up with the contact commands. Same thing happens if I try using getPhysicsCollision and looping through all the objects with the player sprite.

I need to be able to check this, not just to match up sounds but so I can control the player's life accordingly

easter bunny
12
Years of Service
User Offline
Joined: 20th Nov 2012
Playing: Dota 2
Posted: 11th Feb 2014 07:24
Quote: "Maybe Easter Bunny with his/her experience in using physics would make something like this? I imagine quite a few of us would buy it if done well"


I'm flattered

Funnell7
13
Years of Service
User Offline
Joined: 8th Sep 2011
Location: UK, England
Posted: 11th Feb 2014 11:49
One thing I would suggest when using the physics is to change the scale. This gives you more opportunity later on and from my experience, ensures the app works more consistantly on older gen devices.

My platform game used to be on the default scale (which I believe is 0.20) and I had the painful task of having to change everything phsysics related when dropping the scale to 0.05. But it was definately worth it and I will be using 0.05 for all future physic related apps...
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 11th Feb 2014 14:11 Edited at: 11th Feb 2014 14:28
I'd also be happy to write a tutorial if I get the time. If I do I'll add it to this thread or send it to Rick to add to the AppGameKit Bitesize tutorials on the AppGameKit site (or both). Have you tried those examples?

EDIT: Regarding your question about playing a sound when there is a collision; I did achieve a good way of doing this for my Garfield project which had a ball bouncing sound every time the ball hit something. I will dig out my collision detection code when I get home on Thursday unless I still have a copy on dropbox. As I remember it was not an easy business as collisions are not easy to single out as you might expect.

EDIT2: Thinking on it further you could just store the velocity of a sprite in x & y and check for any significant changes in direction. If you get a change in angle you could play a sound at a volume based on the change in total velocity (IE


Of course there's no sound volume is there...

oct(31) = dec(25)
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 11th Feb 2014 17:28 Edited at: 11th Feb 2014 18:23
Quote: "AGK Bitesize tutorials on the AppGameKit site (or both). Have you tried those examples?"

Nope but I'll go take a look now.

I changed my code slightly (but not for the improvement of detection). This let's me more easily check which kinds of sprites have collided.



What's the difference between getContactWorldX and getSpriteContactWorldX? Aside from the latter doesn't seem to return anything after a contact is made.

Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 11th Feb 2014 17:56 Edited at: 11th Feb 2014 18:23
Quote: "Of course there's no sound volume is there..."


There's a volume parameter on the playSound command.

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 11th Feb 2014 20:20
Quote: "There's a volume parameter on the playSound command."

Yes, I meant in my example

Quote: "What's the difference between getContactWorldX and getSpriteContactWorldX?"

GetSpriteContactWorldX is used after using the getSpriteFirstContact command rather than getFirstContact. You are checking EVERY contact in the scene using getFirstContact and every contact for a particular sprite using getSpriteFirstContact.

oct(31) = dec(25)
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 12th Feb 2014 03:57 Edited at: 12th Feb 2014 04:05
So now I'm trying to stop the physics on a bullet after it's colliding with my objects without deleting the sprite. I tried setSpriteActive(spr, 0) but it's still picked up in the physics collisions. Then I tried setSpritePhysicsOff(spr) but that just causes the program to crash as soon as it's called.

Also, it would be nice if we had a command to check if physics was turned on for a sprite.


I've corrected it, though I have no idea why it works. So bullet hit asteroid, asteroid is deleted and replaced with 3 smaller asteroids. If I turn the physics off for the bullet before creating the new asteroids (and thus turning on physics for them), the game crashes. If I create the new asteroids first then deactivate the bullet, it's fine. Which is confusing since after the initial collision, the two sprites have absolutely nothing to do with each other.

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 12th Feb 2014 18:03
Sounds odd... why not just delete the bullet sprite?

oct(31) = dec(25)
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 12th Feb 2014 18:20 Edited at: 12th Feb 2014 18:21
Because then I would have to recreate it every time I shot, and that just doesn't seem very efficient.




baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 12th Feb 2014 20:06 Edited at: 12th Feb 2014 20:08
Creating sprites is very quick unless you're doing hundreds... I wouldn't worry.

EDIT: The game's looking great BTW

oct(31) = dec(25)
Naphier
14
Years of Service
User Offline
Joined: 2nd Oct 2010
Location: St Petersburg, Florida
Posted: 12th Feb 2014 20:16
I used deletion in dFenz for the bullets. It's easier that way and there's no performance hit that I could notice even on old Moto Droid X. The only time I noticed a performance gap was with one of my bosses which is essentially a lump of about 40 sprites. Creating ALL of them within 1 frame caused a bit of a hit, but only a small bump (maybe 200ms) and only on the older devices. My biggest issue was that I had a lot of sprites that I wanted tight physics shapes on and if I updated the shape too often it really hurt the FPS.

Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 14th Feb 2014 19:41
I found a fix for the collision issue when rotating the ship.

Changed this:


To this:


Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 14th Feb 2014 20:06
I'd like to calculate the amount of force of the collisions on the ship. Each size asteroid has a different mass. I thought maybe I could take the ship's new velocity vector after the collision and take the dot product of that with it's old velocity prior to the collision to see the amount of change then factor in the mass somehow, but I don't think that will work accurately.

Ched80
14
Years of Service
User Offline
Joined: 18th Sep 2010
Location: Peterborough, UK
Posted: 16th Feb 2014 11:22
Hey Phaelax,

Yeah forces with sprites is pretty difficult, but what you mention should work, but you don't need the asteroid mass at all.

The force on the ship will be:

F = m*a

where m is the mass of the ship and a is the acceleration. You can figure out a by using your method, but you need to factor in the time difference between the two velocity vectors. i.e.

v1 = old speed vector
t1 = time when v1 was measured
v2 = new speed vector
t2 = time when v2 was measured

a = (v2-v1)/(t2-t1)

AGK will handle the conservation of momentum for you(i.e. the effect of different asteroid mass sizes on your ship's speed) so all you need to do is concentrate on the ship's side of things.

Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 18th Feb 2014 04:06
Oh I think I get what you're saying. Seems easier than I thought, I'll try it!

Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 19th Feb 2014 06:10
Since everything is procedural, I don't think this is quite as accurate as if I was able to have a physics listener object so I could do my calculations exactly at the time of collision rather than wait for my loop to check it, but should be accurate enough.

But I calculated the acceleration vector of the ship after collision and divided its magnitude by the time difference. I've left mass out of it, because I don't really need it. I think the acceleration vector will suffice, multiplying by the mass of the ship at this point will only put the numbers on a larger scale, from a comparative stand point it's just not necessary. (if that makes sense) Either way, I think I have what I needed.

Login to post a reply

Server time is: 2024-11-25 02:32:45
Your offset time is: 2024-11-25 02:32:45