Sonic style physics and collision code?

Non-platform specific questions.

Sonic style physics and collision code?

Postby CrimsonTheDarkBat » Mon Dec 12, 2011 7:26 pm

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! :P
Sonic Velocity Development Thread --> http://game-editor.com/forum/viewtopic.php?f=4&t=11461

Completed Games:
Sonic: War of the Emeralds
Sonic: Empire of Nightmares
Sonic.EXE - The Game
Alice: Crimson Omen
Epsilon 27
User avatar
CrimsonTheDarkBat
 
Posts: 223
Joined: Fri Mar 21, 2008 10:54 am
Score: 20 Give a positive score

Re: Sonic style physics and collision code?

Postby SuperSonic » Mon Dec 12, 2011 9:09 pm

Well, for the speed thing, you could try something like this :D

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;
    }
}
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1443
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score

Re: Sonic style physics and collision code?

Postby CrimsonTheDarkBat » Mon Dec 12, 2011 9:43 pm

Thanks dude it works mostly :D

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? ^^;
Sonic Velocity Development Thread --> http://game-editor.com/forum/viewtopic.php?f=4&t=11461

Completed Games:
Sonic: War of the Emeralds
Sonic: Empire of Nightmares
Sonic.EXE - The Game
Alice: Crimson Omen
Epsilon 27
User avatar
CrimsonTheDarkBat
 
Posts: 223
Joined: Fri Mar 21, 2008 10:54 am
Score: 20 Give a positive score

Re: Sonic style physics and collision code?

Postby skydereign » Mon Dec 12, 2011 10:17 pm

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;
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Sonic style physics and collision code?

Postby CrimsonTheDarkBat » Mon Dec 12, 2011 10:58 pm

Yes that's the code my collisions use at present. How do I rectify that? ^^;
Sonic Velocity Development Thread --> http://game-editor.com/forum/viewtopic.php?f=4&t=11461

Completed Games:
Sonic: War of the Emeralds
Sonic: Empire of Nightmares
Sonic.EXE - The Game
Alice: Crimson Omen
Epsilon 27
User avatar
CrimsonTheDarkBat
 
Posts: 223
Joined: Fri Mar 21, 2008 10:54 am
Score: 20 Give a positive score

Re: Sonic style physics and collision code?

Postby skydereign » Mon Dec 12, 2011 11:04 pm

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.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Sonic style physics and collision code?

Postby CrimsonTheDarkBat » Tue Dec 13, 2011 1:36 pm

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? ^^
Sonic Velocity Development Thread --> http://game-editor.com/forum/viewtopic.php?f=4&t=11461

Completed Games:
Sonic: War of the Emeralds
Sonic: Empire of Nightmares
Sonic.EXE - The Game
Alice: Crimson Omen
Epsilon 27
User avatar
CrimsonTheDarkBat
 
Posts: 223
Joined: Fri Mar 21, 2008 10:54 am
Score: 20 Give a positive score

Re: Sonic style physics and collision code?

Postby Hblade » Tue Dec 13, 2011 2:56 pm

CrimsonTheDarkBat wrote:Thanks dude it works mostly :D

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 :D
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: Sonic style physics and collision code?

Postby CrimsonTheDarkBat » Tue Dec 13, 2011 3:55 pm

Thanks man :D but I've already fixed that ^^; It's the collisions that are causing havoc now like I stated above xD
Sonic Velocity Development Thread --> http://game-editor.com/forum/viewtopic.php?f=4&t=11461

Completed Games:
Sonic: War of the Emeralds
Sonic: Empire of Nightmares
Sonic.EXE - The Game
Alice: Crimson Omen
Epsilon 27
User avatar
CrimsonTheDarkBat
 
Posts: 223
Joined: Fri Mar 21, 2008 10:54 am
Score: 20 Give a positive score

Re: Sonic style physics and collision code?

Postby skydereign » Tue Dec 13, 2011 8:41 pm

Well, I can still make it where the actor doesn't stop or slow down. Is there anything else relevant to your setup?
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Sonic style physics and collision code?

Postby CrimsonTheDarkBat » Tue Dec 13, 2011 8:59 pm

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.
Sonic Velocity Development Thread --> http://game-editor.com/forum/viewtopic.php?f=4&t=11461

Completed Games:
Sonic: War of the Emeralds
Sonic: Empire of Nightmares
Sonic.EXE - The Game
Alice: Crimson Omen
Epsilon 27
User avatar
CrimsonTheDarkBat
 
Posts: 223
Joined: Fri Mar 21, 2008 10:54 am
Score: 20 Give a positive score

Re: Sonic style physics and collision code?

Postby Hblade » Wed Dec 14, 2011 2:38 pm

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.
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: Sonic style physics and collision code?

Postby CrimsonTheDarkBat » Wed Dec 14, 2011 3:01 pm

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. ^^;
Sonic Velocity Development Thread --> http://game-editor.com/forum/viewtopic.php?f=4&t=11461

Completed Games:
Sonic: War of the Emeralds
Sonic: Empire of Nightmares
Sonic.EXE - The Game
Alice: Crimson Omen
Epsilon 27
User avatar
CrimsonTheDarkBat
 
Posts: 223
Joined: Fri Mar 21, 2008 10:54 am
Score: 20 Give a positive score

Re: Sonic style physics and collision code?

Postby skydereign » Wed Dec 14, 2011 9:43 pm

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.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Sonic style physics and collision code?

Postby CrimsonTheDarkBat » Wed Dec 14, 2011 10:19 pm

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. :D
Sonic Velocity Development Thread --> http://game-editor.com/forum/viewtopic.php?f=4&t=11461

Completed Games:
Sonic: War of the Emeralds
Sonic: Empire of Nightmares
Sonic.EXE - The Game
Alice: Crimson Omen
Epsilon 27
User avatar
CrimsonTheDarkBat
 
Posts: 223
Joined: Fri Mar 21, 2008 10:54 am
Score: 20 Give a positive score

Next

Return to General

Who is online

Users browsing this forum: No registered users and 1 guest