Forums

ForumsProgramming Forum

Making a faked physics engine in As2?

Posted Jun 26, '13 at 2:09pm

weirdlike

weirdlike

714 posts

Moderator

_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

 

Posted Jun 26, '13 at 2:35pm

KentyBK

KentyBK

568 posts

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
.
 

Posted Jun 28, '13 at 2:49pm

mightybob

mightybob

369 posts

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)

 

Posted Jul 3, '13 at 10:14am

mightybob

mightybob

369 posts

Bump.

 

Posted Jul 7, '13 at 6:23pm

mightybob

mightybob

369 posts

Anyone?

 

Posted Jul 7, '13 at 8:30pm

weirdlike

weirdlike

714 posts

Moderator

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

 

Posted Jul 7, '13 at 8:33pm

weirdlike

weirdlike

714 posts

Moderator

oops the forums don't allow greater or less than symbols and I don't feel like typing it again...

 

Posted Jul 7, '13 at 11:05pm

weirdlike

weirdlike

714 posts

Moderator

it kind of looks like your "while" statement is going into an infinite loop and freezing up, unlike the "if" statement which runs every "frame", the "while" statement runs independently

trace it

onEnterFrame = function()

{
//initially true every frame...
var whileBool:Boolean = true;

if(whileBool)
{
trace("If Active!&quot;
//make it false, and it still traces
whileBool=false;
}

while(whileBool)
{
trace("While Active!&quot;
//make it false now, so it won't repeat
whileBool=false;
}
}

 

Posted Jul 10, '13 at 1:57pm

mightybob

mightybob

369 posts

Thanks it doesn't freeze anymore now I just need him to keep rotating after 180 degrees, so he can go all the way up the loop.

 

Posted Jul 10, '13 at 2:13pm

weirdlike

weirdlike

714 posts

Moderator

angle = heroMC._rotation;

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

in while statement-> angle += 1;

a simple design, I recommend adding a calculation to incorporate speed in case if too slow climb halfway up and fall down, maybe something like

while statement
{
speed -=.1;
angle = angle+speed*1
}

play around with it and see what you get

 
Reply to Making a faked physics engine in As2?

You must be logged in to post a reply!