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 question

Author
Message
Canadian Mark
12
Years of Service
User Offline
Joined: 28th Jan 2012
Location:
Posted: 5th Feb 2012 02:34
Hi Everyone,

I have a sprite (ball) that when being moved via another sprite (long spinning board) returns a 0 for both the x and y velocity

x# = GetSpritePhysicsVelocityX(Player.spr)
y# = GetSpritePhysicsVelocityY(Player.spr)

Both sprites have the physics set to dynamic
gravity if off
Am I using the wrong code here or do I have to calculate it myself.
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 5th Feb 2012 17:01
Those are the right commands, maybe you are checking a misspelled variable? How are you checking the value of x# and y#?

Canadian Mark
12
Years of Service
User Offline
Joined: 28th Jan 2012
Location:
Posted: 6th Feb 2012 00:06
I have checked my spelling and all is fine(always a great first step)
This is how I'm spinning the board sprite in the center of the screen

SetSpriteAngle( plank.spr, getspriteangle(plank.spr) +2)
SetSpritePositionByOffset(Plank.spr,ScreenWidth /2, ScreenHeight /2)

and I am checking my player sprite velocity on screen like so

Print ("X Velocity = " + str (GetSpritePhysicsVelocityX(Player.spr)) )
Print ("Y Velocity = " + str (GetSpritePhysicsVelocityY(Player.spr)) )

I am using impulses to move the player sprite via user input
i.e. joystick or accelerometer

while being moved by impulses I get values for both velocities,
however when the player sprite is being moved just by the spinning board my velocities return 0's.
The main issue is once the player sprite is thrown or leaves the spinner the physics look very wrong and I'm not sure why I'm not getting the velocity values
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 6th Feb 2012 09:45
Any chance you could post a (not)-working example?

Often in trying to replicate the problem in a simple example I'll find the solution

Canadian Mark
12
Years of Service
User Offline
Joined: 28th Jan 2012
Location:
Posted: 6th Feb 2012 18:48
Sure thing, any help would be appreciated. It's gonna take me a while I have one of those annoying day job things. Should be within 12 hours or so of this post
Canadian Mark
12
Years of Service
User Offline
Joined: 28th Jan 2012
Location:
Posted: 7th Feb 2012 00:55
Ok here is a code example, you'll notice that the velocity for x or y does not change except after a collision or impulse is applied. If you move the cube on to the spinner a few times I think you will see what I mean. Since the cube will travel on an arc these values should change.



It's not pretty but I think it illustrates my point
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 7th Feb 2012 11:42
Well, I see your point. It seems that the velocity of the sprite is only calculated when a collision occurs or an impulse is applied. This is a bug in my opinion. I'll report it as a bug for you.

Canadian Mark
12
Years of Service
User Offline
Joined: 28th Jan 2012
Location:
Posted: 7th Feb 2012 14:40
Thanks alot baxslash for your time with this. I didn't want to got straight to saying 'bug' without a second opinion. I think it may be a Box2D limitation so I guess we'll see what TGC says
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 7th Feb 2012 17:29
My pleasure, and thanks for the beer have one yourself!


Canadian Mark
12
Years of Service
User Offline
Joined: 28th Jan 2012
Location:
Posted: 10th Feb 2012 05:14
TCG any thoughts on this before I breakout the abacus and asprin?
Canadian Mark
12
Years of Service
User Offline
Joined: 28th Jan 2012
Location:
Posted: 10th Feb 2012 09:12
ooops TGC
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 10th Feb 2012 09:47 Edited at: 10th Feb 2012 09:51
A simple way to keep track of the speed of your sprites (as long as there aren't too many of them is to store the previous position of each sprite in an array and get a simple velocity from the two values.

Example:


then in the loop...


EDIT: loop part was missing something...
EDIT 2: ...and slightly incorrect

Basically it's untested so give it a go. You could add in angular velocity without much hassle too.

Paul Johnston
TGC Developer
22
Years of Service
User Offline
Joined: 16th Nov 2002
Location: United Kingdom
Posted: 10th Feb 2012 17:25
Changing the angle and position of physics objects manually tends to result in strange effects. You'll need to set the spinning sprite as a kinematic object (mode 3) and give it an angular velocity to let the physics system know that it is in fact spinning.

The following code should work

Canadian Mark
12
Years of Service
User Offline
Joined: 28th Jan 2012
Location:
Posted: 10th Feb 2012 20:24
Ah ha! That is great information Paul thank you makes alot of sense. I had a feeling that something may have been wrong with my approach to Box2D.

And baxslash you are always more than generous with your time on these posts for many users and it will be remembered thanks
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 10th Feb 2012 20:46
Well, you know I was going to suggest using a kinematic sprite anyway but it didn't occur to me that it might be the cause of the problem!

Quote: "And baxslash you are always more than generous with your time on these posts for many users and it will be remembered thanks"

People have always been helpful on this forum and it's the reason I keep coming back! ...and trying to keep that going

Login to post a reply

Server time is: 2024-11-23 01:12:42
Your offset time is: 2024-11-23 01:12:42