Posted: 24th Sep 2011 2:10
Hi,

I'm trying to make an object bounce higher on certain objects, in order to do this I'm setting the SetSpritePhysicsRestitution with a higher value each time theres a colision with this type of objects..unfortunaly nothing is happening when the colision happens is it some kind of bug? can someone please help?

+ Code Snippet
#include "Main.h"

using namespace AGK;

app App;

int iIndex = 5;
int colision = 0;
char str[50];
float fObjX = 50.0f;
float fObjY = 0.0f;

void app::Begin (void){

	agk::SetVirtualResolution ( 320, 480 );
	agk::EnableClearColor ( 0 );


	int backdrop = agk::CreateSprite ( agk::LoadImage ( "../Data/background1.jpg" ) );
	agk::LoadImage ( 2, "../Data/ball.png" );
	agk::LoadImage ( 3, "../Data/crate.jpg" );


	agk::CreateSprite(3,2);
	agk::SetSpritePosition(3,fObjX, fObjY);
	agk::SetSpriteSize(3,35.0f,35.0f);	
	agk::SetSpriteShape(3, 1);
	agk::SetSpritePhysicsMass(3, 1.5f);
	agk::SetSpritePhysicsOn (3,1);

	agk::CreateSprite (6, 3 );
	agk::SetSpritePosition ( 6, 50.0 - agk::GetSpriteWidth(6)/2.0f, 480.0f -  agk::GetSpriteHeight(6)/2.0f);
	agk::SetSpriteAngle(6,45.0f);
	agk::SetSpritePhysicsOn ( 6, 1 );
	agk::SetSpriteGroup(6,1);

	agk::SetPhysicsDebugOn();
}

void app::Loop(void){

	agk::Print(colision);

	if (agk::GetPointerPressed())
	{
		fObjX = agk::GetPointerX (); 
		fObjY = agk::GetPointerY ();

		agk::SetSpritePosition(3,fObjX,fObjY);

		agk::SetSpritePhysicsDelete(3);
		agk::SetSpritePhysicsOn (3, 2);
	}

	if (agk::GetPhysicsCollision(3,6))
	{
		colision++;
		agk::SetSpritePhysicsRestitution( 3, 10.0f);
	}

	
	agk::Sync();
}

void app::End(void){	
}
Posted: 24th Sep 2011 2:24
I think SetSpritePhysicsRestitution was suppose to be limited to 1.0 as values above can cause issues.
The other thing I noticed is that the second sprite you are colliding with does not have a shape associated with it, that may be causing problems. One other thing you might try is Kinetic for the second Sprite.
These are just suggestions. I'm no expert and still playing around learning how physics work with the AGK.
Posted: 24th Sep 2011 2:30
I don't think you want to increase the restituion, and it looks like you aren't.

You are setting the restitution to 10.0 each time? That is not increasing it.

However, as I understand it, a restitution of 1.0 will bounce an object away at the same speed it hits. When I've increased this value, it only takes a small value to get my sprites moving really quickly, like 1.4, 1.5, etc. The more collisions, the faster the sprite is moving. A value of 10.0 should make the sprite move pretty fast, depending on the scale of your world, sprite mass, etc.

I'm not good enough with C++ to debug your code, but I'll load it into AppGameKit and take a look. Right now, it seems you haven't assigned a shape to sprite 6?

Anyway, I'll give it a try.

EDIT: Hah! Ninja'd by XanthorXIII! Like he said...


Try this in Tier 1, see how the restitution has a large effect at low values over 1.0, and see how the sprite gets erratic when the speed is really fast... I think the speed is capped in there somewhere, but the sprite looks more jerky as the restitution is higher.

+ Code Snippet
iIndex = 5
collision = 0
fObjX = 50.0
fObjY = 0.0

SetVirtualResolution(320, 480)
SetPrintSize(30)

SetPhysicsGravity(0.0, 100.0)
CreateSprite(3,0)
SetSpritePosition(3, fObjX, fObjY)
SetSpriteSize(3, 35.0, 35.0)
SetSpriteShape(3, 1)
SetSpritePhysicsOn(3,2)
SetSpritePhysicsMass(3, 1.5)
SetSpriteAngle(3, 45.0)

CreateSprite(6, 0)
SetSpriteSize(6, 120, 20)
SetSpritePosition(6, 50.0 - GetSpriteWidth(6)/2.0, 480.0 -  GetSpriteHeight(6)/2.0)

SetSpriteShape(6, 1)
SetSpritePhysicsOn(6, 1)
SetSpriteGroup(6,1)

SetPhysicsDebugOn()
restitution# = 1.0
SetSpritePhysicsRestitution(6, 1.0)

do

	Print(str(collision) + "  " + str(restitution#, 2))

	if GetPointerPressed()
        fObjX = GetPointerX()
		fObjY = GetPointerY()
		SetSpritePosition(3, fObjX, fObjY)
		SetSpritePhysicsDelete(3)
		SetSpritePhysicsOn(3, 2)
	endif

	if GetPhysicsCollision(3,6)
		inc collision
		restitution# = restitution# + 0.1
		SetSpritePhysicsRestitution(3, restitution#)
	endif

	Sync()
loop


More Edit: Ah, good, glad you got it sorted!
Posted: 24th Sep 2011 2:54
Thanks guys it worked I've set the SetSpritePhysicsOn of sprite 6 to Kinematic and specified a SetSpritePhysicsRestitution!

+ Code Snippet
#include "Main.h"

using namespace AGK;

app App;

int iIndex = 5;
int colision = 0;
char str[50];
float fObjX = 50.0f;
float fObjY = 0.0f;

void app::Begin (void){

	agk::SetVirtualResolution ( 320, 480 );
	agk::EnableClearColor ( 0 );


	int backdrop = agk::CreateSprite ( agk::LoadImage ( "../Data/background1.jpg" ) );
	agk::LoadImage ( 2, "../Data/ball.png" );
	agk::LoadImage ( 3, "../Data/crate.jpg" );


	agk::CreateSprite(3,2);
	agk::SetSpritePosition(3,fObjX, fObjY);
	agk::SetSpriteSize(3,35.0f,35.0f);	
	agk::SetSpriteShape(3, 1);
	agk::SetSpritePhysicsMass(3, 1.5f);
	agk::SetSpritePhysicsOn (3,1);

	agk::CreateSprite (6, 3 );
	agk::SetSpritePosition ( 6, 50.0 - agk::GetSpriteWidth(6)/2.0f, 480.0f -  agk::GetSpriteHeight(6)/2.0f);
	agk::SetSpriteAngle(6,45.0f);
	agk::SetSpritePhysicsOn ( 6, 3 );
	agk::SetSpriteGroup(6,3);

	agk::SetPhysicsDebugOn();
}

void app::Loop(void){

	agk::Print(colision);

	if (agk::GetPointerPressed())
	{
		fObjX = agk::GetPointerX (); 
		fObjY = agk::GetPointerY ();

		agk::SetSpritePosition(3,fObjX,fObjY);

		agk::SetSpritePhysicsDelete(3);
		agk::SetSpritePhysicsOn (3, 2);
	}

	agk::SetSpritePhysicsRestitution( 3, 0.5f);
	agk::SetSpritePhysicsRestitution( 6, 5.0f);
	
	agk::Sync();
}

void app::End(void){	
}