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 / Struggling with physics

Author
Message
nz0
AGK Developer
17
Years of Service
User Offline
Joined: 13th Jun 2007
Location: Cheshire,UK
Posted: 20th Feb 2012 22:35
I'm trying to move a sprite around using physics.
I've set zero gravity, so I have full control over all movement and seem to be having trouble making a sprite move in one direction without spinning out of control.
I will want to move my 32pixel high sprite directionally (like a missile) but like:

SetSpritePhysicsForce(s,Thrust#*-1000.0,0.0,Thrust#*1000.0,0.0)

where 0.0 I have set 16.0 with no effect (where's the origin again?) It still spins...

I'm not even sure this is going to a sensible way to do this now, as I can't set a rotation - I have to use a rotational impulse or similar.

If I could be sure of sending an impulse through the origin and avoid rotating, I would probably be OK.

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 20th Feb 2012 22:50
Try it like this:
SetSpritePhysicsForce(s,getSpriteXbyOffset(s),getSpriteYbyOffset(s),forceX#,forceY#)

You need to apply the force to the sprite's centre of gravity (it's offset)

nz0
AGK Developer
17
Years of Service
User Offline
Joined: 13th Jun 2007
Location: Cheshire,UK
Posted: 20th Feb 2012 23:24
getSpriteXbyOffset() helps a lot

nz0
AGK Developer
17
Years of Service
User Offline
Joined: 13th Jun 2007
Location: Cheshire,UK
Posted: 20th Feb 2012 23:32
Somehow, i thought the force had to be a "line" through the sprite. That actually solves a lot of problems I've had in the past too.

Be nice if there was a force effect for a direction (angular)

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 20th Feb 2012 23:39
Quote: "Be nice if there was a force effect for a direction (angular)"

I've added it as an enhancement request on the bug board about a month ago

nz0
AGK Developer
17
Years of Service
User Offline
Joined: 13th Jun 2007
Location: Cheshire,UK
Posted: 21st Feb 2012 00:18 Edited at: 21st Feb 2012 00:21
Well, I've got "something" working for tonight.
Need to tweek a bit more and wonder why my rotation doesn't work properly.

Seems to do a full opposite spin when circumventing the 0/360 boundary.
I've tried:

if angle#>360 then angle#=angle#-360, but it doesn't seem to matter..

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 21st Feb 2012 01:30
Try changing the "if" into a "while" maybe?

What are you trying to get it to do precisely?

nz0
AGK Developer
17
Years of Service
User Offline
Joined: 13th Jun 2007
Location: Cheshire,UK
Posted: 21st Feb 2012 02:14
Just trying to fly a little 2d plane about with physics. An ideal mix here would be a thruster - particle emitter meets physics

I was just experimenting with physics to see if it was worth doing that way, or the old fashioned way.

basjak
14
Years of Service
User Offline
Joined: 16th Apr 2010
Location: feel like signing up for mars
Posted: 21st Feb 2012 03:40
Quote: "i thought the force had to be a "line" through the sprite"

it means that the force vector is parallel with the line from the centre of the sprite to the force position. other wise the force will cause body rotation.

I wrote this example on two sprites:
1, rocket: offset and body mass is in the top centre of the sprite image.
2, saw: offset and body mass is in the centre of the sprite image.

notice that no rotation happen when you balance your sprite inner physics.

download the full file example I made for you.

Attachments

Login to view attachments
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 21st Feb 2012 09:20
Quote: "I was just experimenting with physics to see if it was worth doing that way, or the old fashioned way."

Using physics is an easy way to deal with collision for many game genres. It is more than possible. I would suggest that for an aeroplane you might be better setting the velocity rather than applying forces. You would have to check for collisions with scenery etc.

Since you probably never want your plane to bounce off anything you might be better using the traditional method though, just position the sprite and check for collisions...

Login to post a reply

Server time is: 2024-11-23 04:19:54
Your offset time is: 2024-11-23 04:19:54