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 / restitution problem

Author
Message
kirtn14
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 2nd Jan 2013 17:56
im making the game 'pong' as an experiment to gain knowlege and skill but i cant seem to get the restitution to work, the ball just speeds up and bounces up and down without stopping, how do i change this?

kirtnicholls
TrezSoft
AGK Tool Maker
11
Years of Service
User Offline
Joined: 8th Nov 2012
Location: UK
Posted: 2nd Jan 2013 18:03
Have you set the Damping ? SetSpritePhysicsDamping(spriteid,fdamp)
by default damping is set to 0 no damping.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 2nd Jan 2013 18:39
What value are you using in the SetSpritePhysicsRestitution command? If it is greater than 1.0, your sprite gains energy every time it bounces instead of losing energy.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
kirtn14
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 2nd Jan 2013 21:44
what value would you advise using to keep the ball moving but not stopping?

kirtnicholls
The Zoq2
14
Years of Service
User Offline
Joined: 4th Nov 2009
Location: Linköping, Sweden
Posted: 2nd Jan 2013 21:57
If you want it to move at the same speed before the colision as after, you want to set it to 1.0 go lower and it will loose speed in time and higher to make it gain speed each bounce
kirtn14
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 2nd Jan 2013 23:59
thanks how do i get it to move fast at first?

kirtnicholls
The Zoq2
14
Years of Service
User Offline
Joined: 4th Nov 2009
Location: Linköping, Sweden
Posted: 3rd Jan 2013 00:12
You could use "SetSpriteVelocity" I think
Daniel TGC
Retired Moderator
17
Years of Service
User Offline
Joined: 19th Feb 2007
Location: TGC
Posted: 3rd Jan 2013 02:26 Edited at: 3rd Jan 2013 02:28
Why are you using physics for pong? It seems overkill. All you have to do is increase or decrease values of X and Y by a specific speed.

For example.

X# = X# + SpeedX#
X# = X# - SpeedX#

If collision detected then SpeedX# = SpeedX# + 0.05

Y# = Y# + SpeedX#
Y# = Y# - SpeedX#

If collision detected then SpeedY# = SpeedY# + 0.05



That above AppGameKit tutorial demonstrates this basic logic.
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 3rd Jan 2013 11:38
Quote: "Why are you using physics for pong? It seems overkill. All you have to do is increase or decrease values of X and Y by a specific speed."

In principle I agree with you Daniel but I have used physics for my pong game and it made life a tonne easier. I just give the sprite a velocity using setSpritePhysicsVelocity as suggested above, then just let the physics system do the rest. I found a restitution value between 0.9 and 0.99 worked best but you'll have to play. I found if I got too close to 1.0 it occasionally increased the speed of the ball. You could check that the ball is within a certain speed min/max and enforce it using setSpritePhysicsVelocity again too.

Doing it this way you can add barriers and other objects that interact with the ball with no extra runtime code.

There are a lot of ways of skinning this cat using AppGameKit do whatever feels right and if it doesn't work try something else.


this.mess = abs(sin(times#))
Daniel TGC
Retired Moderator
17
Years of Service
User Offline
Joined: 19th Feb 2007
Location: TGC
Posted: 3rd Jan 2013 12:46
I wonder which requires more processing, thus more battery/ cpu cycle time. Simply working with X and Y directly, then placing the sprites at that position, or initialising a full physics engine to deal with it. I'll have to test that one when I'm not quite so busy.
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 3rd Jan 2013 12:48
I would imagine your way would be less intensive but box2d is really damned fast. I imagine the difference in a pong game would be next to nothing. I would be interested in your results though


this.mess = abs(sin(times#))
kirtn14
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 3rd Jan 2013 22:32
what did you do when the sprite was hit to make it disapear but have the ball bounce off?

kirtnicholls
Daniel TGC
Retired Moderator
17
Years of Service
User Offline
Joined: 19th Feb 2007
Location: TGC
Posted: 4th Jan 2013 02:59
Sorry was that directed at me or someone else?
kirtn14
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 5th Jan 2013 16:04
general question

i thought i could try something like



i hvan't tried it yet it was just an idea, i set the position to 1000, 1000 so that when al the sprites are gone i can move them to a new position making it look like a new level

kirtnicholls
kirtn14
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 10th Feb 2013 17:16
the ball works for a while then stops bouncing and slows down? and i dont quiet understand the example you gave Daniel TGC

kirtnicholls
DVader
20
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 11th Feb 2013 18:01
Make sure you have your bats/walls and ball are set to restitution 1 or a little less if you want the ball to keep moving all the time using physics. Possibly you may want to set the friction values also. Still Daniel is quite right, a basic Pong game would work fairly well just reversing your balls x and y direction when the ball hits the bat, and just the y axis on the walls.

kirtn14
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 3rd Mar 2013 16:39
the restitution works but not very well. how would i do what Daniel talked about?

kirtnicholls
kirtn14
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 10th Mar 2013 13:48
how would i do that, i cant seem to change the movement of a sprite once i have used
Quote: "setsrpiteposition (sprites[1],x#,y#)
x#=x#+0.2"


kirtnicholls
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 10th Mar 2013 16:16
Using physics to move things and setspriteposition at the same time cancel out all of the physics stuff.

If you want to use physics you would do something more along the lines of this:


Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
xGEKKOx
AGK Master
15
Years of Service
User Offline
Joined: 7th Dec 2008
Location: Italy
Posted: 11th Mar 2013 01:01
When i did my Match Point : Online Ping Pong on iOS i started with physic, but after 2 days, i returned to the X and Y velocity.
Better calculate directly what you need instead of using all the Physic engine.
Especially if you wanna do a multiplayer like i done.

Long life to Steve!
kirtn14
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 11th Mar 2013 13:30
there is conflict with the origional setspritephysicsvelocity and the new one on my game, i have done this

Quote: "
//rem to make variables for bouncing
for i# = 0.05 to 0.25
x# = x# + i#
xx# = xx# - i#

y# = y# + i#
yy# = yy# - i#
next i#


//rem to make sure the ball can go any way
a = x# OR xx#
b = y# OR yy#


//rem ball hit pannel
if getphysicscollision (sprites[2],sprites[3])
setSpritePhysicsVelocity (sprites[2],x#,yy#)
endif
"


i want the ball to move in different directions at differnt speeds. i have used "A" for things such as bricks allowing the ball to go in any direction.. well in theory

kirtnicholls
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 11th Mar 2013 16:21
"for i# = 0.05 to 0.25" this will cause you a problem unless you add the step value to use. E.g. "for i# = 0.05 to 0.25 step 0.05".

And your a and b logic statements won't work. Using a float value with 'OR' doesn't really work correctly. Any non-zero value is true and you don't show anywhere that the variables are zero. It is also very hard to get exactly zero in a float unless you explicitly assign zero.

And you must not have included a lot of code, because what you just showed doesn't look right.

The for loop doesn't appear to do anything and assumes that the variables (other than i#) have started with some value. The loop just changes the values some unknown amount of times (probably only once, since the default increment in a for loop is 1). And you don't show how you are using the 'a' and 'b' variables.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 11th Mar 2013 20:04
Totally agree with AL. Using logical operators on floating point values is a disaster recipe.

-- Jim DO IT FASTER, EASIER AND BETTER WITH AppGameKit FOR PASCAL
kirtn14
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 11th Mar 2013 20:17
I used "a and b" in collision with a brick to get the ball to move in any direction

setspritephysicsvelocity (sprites[2],a,b)

kirtnicholls
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 11th Mar 2013 20:45
Quote: "setspritephysicsvelocity (sprites[2],a,b)"

That's different from your previous code post.

And, in this call, you would be passing in whole integer values, so your fractions would be lost.

I'm guessing your intent is to randomly pick either x# or xx# for the 'a' and y# or yy# for the 'b'. Since the values for x/y are just opposite signs of each other, you can calculate just the positive value and then multiply it by RandomSign(1) (which will return either 1 or -1).

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
kirtn14
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 11th Mar 2013 22:53
So just do x#/ y# then call "randomsign (x#)" instead of xx#/yy#,and a/b?

kirtnicholls
kirtn14
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 11th Mar 2013 22:54
That piece of script is different as the first one,was for the paddle, there's a new on for each wall, the paddle and the bricks (6 in total)

kirtnicholls
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 12th Mar 2013 03:24
"randomsign(1)*x#" is what you want since randomsign only returns an integer value.

But, yes, that probably will do what you want. If I understand what you were trying to do in the fragments you showed correctly.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
kirtn14
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 12th Mar 2013 11:26
Quote: "

//rem to make variables for bouncing
for i# = 0.05 to 0.25
x# = x# + i#
xx# = xx# - i#

y# = y# + i#
yy# = yy# - i#
next i#


//rem to make sure the ball can go any way
a = x# OR xx#
b = y# OR yy#


//rem ball hit pannel
if getphysicscollision (sprites[2],sprites[3])
setSpritePhysicsVelocity (sprites[2],x#,yy#)
endif

//rem ball hit bricks
for i = 1 to 21
if getspritecollision (sprites[2],brick[i])
setSpritePhysicsVelocity (sprites[2],a,b)
endif
next i


//rem ball hit wall left
if getspritecollision (sprites[2],sprites[4])
setSpritePhysicsVelocity (sprites[2],x#,getspritey (sprites[2]))
endif

//rem ball hit wall right
if getspritecollision (sprites[2],sprites[5])
setSpritePhysicsVelocity (sprites[2],xx#,getspritey (sprites[2]))
endif



//rem ball hit wall bottom
if getspritecollision (sprites[2],sprites[6])
setSpritePhysicsVelocity (sprites[2],getspritex (sprites[2]),y#)
endif
"


kirtnicholls
kirtn14
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 12th Mar 2013 11:38
changes that code to



i think ive done it wrong as the ball just moves about in any and every deiection without touching anything

kirtnicholls
kirtn14
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 12th Mar 2013 11:42
i know the issue, its the walls. i iscolated the code and it sort of works

im now left with



kirtnicholls
JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 12th Mar 2013 12:19
Your FOR loop at the top of the last code is seriously flawed. Are you looping with i or i#? And you have not put a step command in. If it does anything at all it will add 0.19 to x# and y#. I think the FOR command defaults to 1 as an increment.

-- Jim DO IT FASTER, EASIER AND BETTER WITH AppGameKit FOR PASCAL
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 12th Mar 2013 15:17
The for loop was the first thing I commented on.

If you are doing a for loop with float values, you MUST provide a step value to increment by. Otherwise it will add 1 and the loop only occurs once, adding 0.19 to the x# and y# and stopping.

I am going to have to assume that the code you posted is happening inside the main loop or in a function called in the main loop and that the x# and y# variables have been initialized before the loop.

In order to keep the floating point values and make sure that the calculations happen in the correct order, change this:


to this:


(And drop the space between all instances of 'randomsign' and '(1)' just to be sure the compiler doesn't get confused.)

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
kirtn14
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 12th Mar 2013 19:54
I apologise for asking so many questions, I've only started to read the guide book (i copied all of the agk documentation pages and had them printed index book after making an index, not available to anyone except me)

kirtnicholls
kirtn14
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 14th Mar 2013 19:38
ive got it down to this now and the results are better but they arent consistant and the ball seems to gain energy and have a mad attack speeding up and shake

setspriteshape (sprites[2],1)


setspritephysicson (sprites[2],2)
setspritephysicson (sprites[3],1)

for i = 1 to 21
setspritephysicson (brick[i],1)
next i

do

//rem to make variables for bouncing
x# = x# + 0.1
y# = y# + 0.1


//rem ball hit pannel
if getphysicscollision (sprites[2],sprites[3]) = 1
setSpritePhysicsVelocity (sprites[2],(randomsign (1)*(x#/3.0)),(-y#))
endif


//rem ball hit bricks
for i = 1 to 21
if getphysicscollision (sprites[2],brick[i]) = 1
setSpritePhysicsVelocity (sprites[2],(randomsign (1)*x#/3.0),(randomsign (1)*y#/10.0))
endif
next i

kirtnicholls
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 14th Mar 2013 20:17
What do you have Friction, Restitution and Damping set to?

If you use a value greater than 1.0 in SetSpritePhysicsRestitution, it gains energy with each bounce, instead of losing it. This is actually pretty cool and quite amusing, but maybe not what you want.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
kirtn14
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 14th Mar 2013 22:45
At this point in time all three are non existent in the game,amusing how shocking my "skills" are :L

kirtnicholls
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 15th Mar 2013 02:33
If you haven't set them, then that isn't the issue. They default to reasonable values.

Did you set the masses on any of your sprites? (Not usually done, but could have an affect.)

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
lilpissywilly
AGK Developer
13
Years of Service
User Offline
Joined: 10th Sep 2010
Location: Office Chair
Posted: 18th Mar 2013 12:48
This is an odd approach and "wrong" in several places. Either decide if you want box2d to handle your reflections off of sprites or code your own, this is like a mix. I can cook up some code when I'm at my computer.

The reason your ball gains speed is because you are increasing the value by which you set velocity every frame. "y# = y# + 0.1". Drop that line and that problem is gone

My hovercraft is full of eels

Login to post a reply

Server time is: 2024-05-07 17:05:05
Your offset time is: 2024-05-07 17:05:05