Page 1 of 3
Sonic style physics and collision code?
Posted:
Mon Dec 12, 2011 7:26 pm
by CrimsonTheDarkBat
Hey guys, been working on the template for my next Sonic game and I have a quick request ^^;
If anyone knows of a good Sonic style code for movement (for example, the player grows faster as he runs, but reaches a certain speed limit after some time. Also, he gently decelerates rather than coming to a halt as soon as the player depresses the key) and just for the sake of it - the best code to use for collisions? Thanks very much and all that!
Re: Sonic style physics and collision code?
Posted:
Mon Dec 12, 2011 9:09 pm
by SuperSonic
Well, for the speed thing, you could try something like this
Global Code:- Code: Select all
#define MaxSpeed 10
#define IncrementSpeed 0.5
int Dir = 0;//The direction that sonic is moving in. 1 for right, -1 for left, and 0 for stopped.
Sonic -> KeyDown -> Right- Code: Select all
Dir = 1;//moving left
Sonic -> KeyDown -> Left- Code: Select all
Dir = -1;//moving right
Sonic -> KeyUp -> (Do this event twice for left key and right key)- Code: Select all
Dir = 0;//stopped
Sonic -> Draw Actor- Code: Select all
if(Dir == 1)
{
if(xvelocity < MaxSpeed)//If sonic is not going as fast as he can...
{
xvelocity += IncrementSpeed;//...then speed him up.
//You can also put animation code here ;)
}
}
else if(Dir == -1)
{
if(xvelocity > MaxSpeed)
{
xvelocity -= IncrementSpeed;//This will also speed him up. But in the opposite direction :D
//Again, you can put animation code here
}
}
else//If Dir is not equal to 1 or -1, then it must be equal to 0
{
if(xvelocity > 0)
{
xvelocity -= IncrementSpeed;
}
else if(xvelocity < 0)
{
xvelocity += IncrementSpeed;
}
}
Re: Sonic style physics and collision code?
Posted:
Mon Dec 12, 2011 9:43 pm
by CrimsonTheDarkBat
Thanks dude it works mostly
However only in one direction :/ Sonic can only move right correctly - left dosent budge one bit ^^;
Also, when Sonic collides with different section of the floor (separate floor actor) he stops slightly :/ any more ideas? ^^;
Re: Sonic style physics and collision code?
Posted:
Mon Dec 12, 2011 10:17 pm
by skydereign
You need to set the left direction's if statement to this (xvelocity > -MaxSpeed). And what are you doing for your collisions? Are you conserving xvelocity like this?
player -> Collision with Top Side of tiles -> Script Editor
- Code: Select all
double xvel = xvelocity;
PhysicalResponse(MOVE_EVENT_ACTOR_ONLY, USE_CALCULATED_MASS, 1.000000,1.000000, 0.000000, 0.000000);
xvelocity = xvel;
Re: Sonic style physics and collision code?
Posted:
Mon Dec 12, 2011 10:58 pm
by CrimsonTheDarkBat
Yes that's the code my collisions use at present. How do I rectify that? ^^;
Re: Sonic style physics and collision code?
Posted:
Mon Dec 12, 2011 11:04 pm
by skydereign
Can you upload a picture of the two actors, or explain it a bit more? Just from the information provided here, I can recreate it with no problem.
Re: Sonic style physics and collision code?
Posted:
Tue Dec 13, 2011 1:36 pm
by CrimsonTheDarkBat
I'm on my iPhone so can't upload any shots right now :C but basically, there are separate actors
for two sections of the floor (eg; one for flat ground, one for an upward slope, etc) d'ya get it? ^^
Re: Sonic style physics and collision code?
Posted:
Tue Dec 13, 2011 2:56 pm
by Hblade
CrimsonTheDarkBat wrote:Thanks dude it works mostly
However only in one direction :/ Sonic can only move right correctly - left dosent budge one bit ^^;
Also, when Sonic collides with different section of the floor (separate floor actor) he stops slightly :/ any more ideas? ^^;
- Code: Select all
if(Dir == 1)
{
if(xvelocity < MaxSpeed)//If sonic is not going as fast as he can...
{
xvelocity += IncrementSpeed;//...then speed him up.
//You can also put animation code here ;)
}
}
else if(Dir == -1)
{
if(xvelocity > -MaxSpeed)
{
xvelocity -= IncrementSpeed;//This will also speed him up. But in the opposite direction :D
//Again, you can put animation code here
}
}
else//If Dir is not equal to 1 or -1, then it must be equal to 0
{
if(xvelocity > 0)
{
xvelocity -= IncrementSpeed;
}
else if(xvelocity < 0)
{
xvelocity += IncrementSpeed;
}
}
A fix :3 he forgot a - in the if (xvelocity > -MaxSpeed) part
Re: Sonic style physics and collision code?
Posted:
Tue Dec 13, 2011 3:55 pm
by CrimsonTheDarkBat
Thanks man
but I've already fixed that ^^; It's the collisions that are causing havoc now like I stated above xD
Re: Sonic style physics and collision code?
Posted:
Tue Dec 13, 2011 8:41 pm
by skydereign
Well, I can still make it where the actor doesn't stop or slow down. Is there anything else relevant to your setup?
Re: Sonic style physics and collision code?
Posted:
Tue Dec 13, 2011 8:59 pm
by CrimsonTheDarkBat
Well basically I've got the player actor moving correctly using the code above, but when he collides with a separate floor actor (eg; moving from a flat piece of land to a sloped piece of land) then he stops and starts moving again as though the player depressed the key and pressed it again. All collisions in the game use the code skydereign mentioned above.
Re: Sonic style physics and collision code?
Posted:
Wed Dec 14, 2011 2:38 pm
by Hblade
CrimsonTheDarkBat wrote:Well basically I've got the player actor moving correctly using the code above, but when he collides with a separate floor actor (eg; moving from a flat piece of land to a sloped piece of land) then he stops and starts moving again as though the player depressed the key and pressed it again. All collisions in the game use the code skydereign mentioned above.
This is because your using a physical response of 0 and using Xvelocity to move. Alter the code a bit to replace xvelocity with x. Replacing xvelocity with X would not work. You would need to create another variable that gets added instead of xvelocity, then have x+=variable to move.
Re: Sonic style physics and collision code?
Posted:
Wed Dec 14, 2011 3:01 pm
by CrimsonTheDarkBat
Hmm I think I understand, but not 100% sure. ^^;
If you could in fine detail explain what I have to do or if there's an alternate collision code I can use, let me know. ^^;
Re: Sonic style physics and collision code?
Posted:
Wed Dec 14, 2011 9:43 pm
by skydereign
That may fix it, but if it does that means you have some collision event that is reseting xvelocity to 0. I suspect you have a wall (left right) collision with the new platform? That one would have similar code as the xvelocity collision, but instead conserve yvelocity. The problem with using x+= is that you need to maintain another variable and treat it like xvelocity. So if you could do it without having making a duplicate xvelocity generally it'd be better. But, you'd have to sort out your collisions.
Re: Sonic style physics and collision code?
Posted:
Wed Dec 14, 2011 10:19 pm
by CrimsonTheDarkBat
Alrighty, I'm boggled about all of this - I don't understand anything ^^;
If you could copy + paste the code I need and tell me if there's any changes to be made to it, the that'd be great.