ForumsProgramming ForumMaking a faked physics engine in As2?

39 31924
mightybob
offline
mightybob
360 posts
Peasant

I want to make a faked physics engine in As2 like Sonic or Fancy Pants. So far all I got is gravity, when you hit the floor you stop going down, move left and right, and when you hit a steep enough slope you can't go up it.

But what I am currently trying to do is make it so the player can go through loops, or go up upside-down ramps, like this: [url=http://i39.tinypic.com/2uti2d4.jpg]

Any help would be appreciated!

  • 39 Replies
KentyBK
offline
KentyBK
566 posts
Nomad

I recommend you give this article a look, just to get the basic idea for all your loopy goodness.

weirdlike
offline
weirdlike
1,299 posts
Prince

Flade

good luck

mightybob
offline
mightybob
360 posts
Peasant

@KentyBK That article is just talking about Sonic's "amazingness". And who knows if it's talking about As2, As3, or javascript.

KentyBK
offline
KentyBK
566 posts
Nomad

@KentyBK That article is just talking about Sonic's "amazingness". And who knows if it's talking about As2, As3, or javascript.


Uhhhh no. It explains how the game's engine works, including how the game handles slopes and curved surfaces. It's written in pseudocode so that you can adapt it to whatever language you want. It seemed appropriate given that you want to make an engine "like" those games.

I'm not going to post ready-made code, because that's your job.
mightybob
offline
mightybob
360 posts
Peasant

I don't want ready made code, or links to ready made code.

Secretmapper
offline
Secretmapper
1,747 posts
Nomad

If not, then KentyBK's link is an amazing resource really.

mightybob
offline
mightybob
360 posts
Peasant

Sorry, was just really frustrated.

I looked through the link you gave me, it helped a lot.

So far I got my player to go halfway up the loop, but then because of using "_y -= gravity" to make it so he hits the ground, it makes the player shoot up to the top of the ground. So, I replaced that with: "_y -= Math.sin(_rotation * Math.PI / 180) * gravity;" into a "while" statement. But now whenever the player hits the ground, Flash Player freezes. I have encountered this problem A LOT of times,
and am getting pretty tired of it ruining every fix i come up with. (I tried an "if" statement too, froze Flash Player also).

Will "_y -= Math.sin(_rotation * Math.PI / 180) * gravity;" even work? And if so how can I fix this "while" statement problem?

weirdlike
offline
weirdlike
1,299 posts
Prince

I'm not sure what the exact calculation would be but you can look at this

speed = 5;
gravity = 2;
angle = MC._rotation;

dx = Math.cos(angle*Math.PI/180);
dy = Math.sin(angle*Math.PI/180);


MC.onEnterFrame = function()
{
this.dy += gravity;
this._x += this.dx*speed;
this._y += this.dy*speed;
}

mightybob
offline
mightybob
360 posts
Peasant

Well, I still have the problem of Flash Player freezing when using my gravity var with math in a while or if statement. I used math.cos and math.sin in other if statements, but for some reason it's freezing in just this one... The only code I used in my while statement was this:

_y -= Math.sin(_rotation * Math.PI / 180) * gravity;
Grav = 0;

And I tried to do so with an if statement too. But it still froze. Tried googling it, could only find as3 results. But then when I just use

Grav = 0;
_y -= gravity;

No freezing, but halfway up the loop the player shoots up.

So I'm pretty sure _y -= Math.sin(_rotation * Math.PI / 180) * gravity; will work, all I need is a fix for flash freezing when I put it In a while statement.

If you need more of my code to help figure out the problem, just let me know.

Ps: if this helps at all, it's not timeline code, it's code inside of a symbol.

weirdlike
offline
weirdlike
1,299 posts
Prince

_y -= Math.sin(_rotation * Math.PI / 180) * gravity;
Grav = 0;

if this is what I think it is use order of operation

Grav = 0;
_y -= Math.sin(_rotation * Math.PI / 180) * gravity;

also if
Math.sin(_rotation * Math.PI / 180) = 10

then 10*0 = 0

it wont work try tracing the calculation

trace(Math.sin(_rotation * Math.PI / 180) * gravity);

just my observations

KentyBK
offline
KentyBK
566 posts
Nomad

So I'm pretty sure _y -= Math.sin(_rotation * Math.PI / 180) * gravity; will work

I'm not sure it does, what do you WANT it to do? Right now, _y won't decrease at all when _rotation is 180 or 0 degrees. Similarly, it evaluates to _y -= gravity and _y -= - gravity for 90 and 270 degrees, respectively.


If you need more of my code to help figure out the problem, just let me know.


I'd suggest posting the condition inside your while statement and most of the relevant code, simply because it's always easier to read in context
.
mightybob
offline
mightybob
360 posts
Peasant

Here is my code I wrote: (all of this is inside my symbol with the instance name of "char&quot

onClipEvent(load) {
var speed:Number = 0;
var maxSpeed:Number = 23;
var char:MovieClip = _root.char;
var grav:Number = 0;
var gravity:Number = 2;
var touchingGround:Boolean;
var hittingWall:Boolean;
var leftOrRightDown:Boolean = false;
var ground:MovieClip = _root.ground;
var boolean:Boolean;
var otherBoolean:Boolean;
var maxJump:Number = -30;
var collision:Boolean;
var collision2:Boolean;
var wall:MovieClip = _root.wall;
var leftJumpReady:Boolean;
var rightJumpReady:Boolean;
}

onClipEvent(enterFrame) {

_y += grav;
grav += Math.cos(_rotation * Math.PI / 180) * gravity;
grav += Math.sin(_rotation * Math.PI / 180) * gravity;

if (leftJumpReady && touchingGround == false)
{
if(grav > 0)
{
grav -= 1;
}

if(Key.isDown(Key.SPACE))
{
speed = -20;
grav = maxJump;
}
}

while (ground.hitTest(_x, _y, true))
{
grav = 0;
}

if (ground.hitTest(_x+5, _y+5, true))
{
touchingGround = true;
}

if (ground.hitTest(_x, _y, true))
{
touchingGround = true;
}

else {
touchingGround = false;
}

this is the while statement making it freeze while (ground.hitTest(_x, _y, true))
{
_y -= Math.sin(_rotation * Math.PI / 180) * gravity;
}



if (wall.hitTest(_x+(_width/1), _y-(_height/1), true))
{
_x -= speed;
hittingWall = true;
rightJumpReady = true;
}

else {
hittingWall = false;
rightJumpReady = false;
}

if (wall.hitTest(_x-(_width/1), _y-(_height/1), true))
{
_x += speed;
hittingWall = true;
leftJumpReady = true;
}

else {
hittingWall = false;
leftJumpReady = false;
}

if(Key.isDown(Key.LEFT))
{
_x -= Math.cos(_rotation * Math.PI / 180) * speed;
_y -= Math.sin(_rotation * Math.PI / 180) * speed;
if(speed = 0 && !Key.isDown(Key.RIGHT))
{
_x -= speed;
speed -= 2;
}
}

if(Key.isDown(Key.SPACE) && touchingGround)
{

grav = maxJump;
}

if(Key.isDown(Key.DOWN) && touchingGround == false)
{
grav += 15;
}

if(touchingGround == false)
{
gotoAndStop("jump&quot
}

if(collision == false && collision2 == false)
{
if(_rotation > 0)
{
_rotation -= 1;
}

if(_rotation < 0)
{
_rotation += 1;
}
}


if(Key.isDown(Key.LEFT) && touchingGround)
{
gotoAndStop("run&quot;
_xscale = -100;
}

if(Key.isDown(Key.RIGHT) && touchingGround)
{
gotoAndStop("run&quot;
_xscale = 100;
}

if (ground.hitTest(_root.side._x, _root.side._y, true) && Key.isDown(Key.RIGHT) && touchingGround)
{
_rotation -= 1;

}

else {

}

if (ground.hitTest(_root.otherSide._x, _root.otherSide._y, true) && Key.isDown(Key.LEFT) && touchingGround)
{
_rotation += 1;

}

else {

}

if (ground.hitTest(_root.side._x, _root.side._y, true))
{
collision = true;
}

else {
collision = false;
}

if (ground.hitTest(_root.otherSide._x, _root.otherSide._y, true))
{
collision2 = true;
}

else {
collision2 = false;
}
}


otherSide is the left side of char, side is the right side of char.

collision2 and collision booleans are to check if side or otherSide are hitting ground.

ground is the instance name of my ground symbol... Obviously.

And what I am trying to get it to do is:

I want the player to be able to go up crazy ramps, sorta like in the Fancy Pants Adventures. (see linked image below)



And also what I am trying to do is jump at angles, because if I figure out how to do this whole crazy ramp thing not being able to jump at angles will cause a big problem.

A link to a tutorial or any help would be great.

Thanks!

(PS: just ignore rightJumpReady and leftJumpReady, it's just my failed attempt at wall jumps)

mightybob
offline
mightybob
360 posts
Peasant

Bump.

mightybob
offline
mightybob
360 posts
Peasant

Anyone?

weirdlike
offline
weirdlike
1,299 posts
Prince

I prefer to use the key input on the main timeline. Move the background instead of the character example tutorial here.

some things I like to try

[i]grav -= .5;
if (grav

Showing 1-15 of 39