Forum Topic: Physics

(838 views • 47 replies)

New Topic Post Reply

This topic is 2 pages long. [ 1 | 2 ]

<< < > >>
Angry

7IsUnlucky

Reply to Post Reply & Quote

Posted at: 10/24/09 10:23 PM

7IsUnlucky FAB LEVEL 25

Sign-Up: 06/12/07

Posts: 1,965

I'm trying to make my own physics engine in Actionscript 2, but I can't figure out a way of having the ball slow down in mid-air and stop
This is 30 fps, ball mc is "ball", platform is "platform" (lol)

onClipEvent(load){
	xspd = 0;
	yspd = 0;
	down = true;
	limity = yspd;
}
onClipEvent(enterFrame){
			_y += yspd;
	if(down == true){
		yspd += .7;
	}
		if(down == false){
		yspd -= .7;
	}
	if(this.hitTest(_root.platform)){
		yspd = -10;
		down = false;
	}
}

And don't give me any shitty code I can copy and paste; I want pseudo code so I can actually understand wtf I'm doing.

Programming languages I know: AS2 and TRS-80 BASIC

BBS Signature

None

Toast

Reply to Post Reply & Quote

Posted at: 10/24/09 11:12 PM

Toast DARK LEVEL 09

Sign-Up: 04/02/05

Posts: 8,924

wtf?
velocity += acceleration
_y += velocity
velocity *= -1 when hit floor


None

7IsUnlucky

Reply to Post Reply & Quote

Posted at: 10/25/09 12:17 AM

7IsUnlucky FAB LEVEL 25

Sign-Up: 06/12/07

Posts: 1,965

Doesn't work

Programming languages I know: AS2 and TRS-80 BASIC

BBS Signature

None

7IsUnlucky

Reply to Post Reply & Quote

Posted at: 10/25/09 01:08 AM

7IsUnlucky FAB LEVEL 25

Sign-Up: 06/12/07

Posts: 1,965

You guys are so helpful

Got most of the kinks out by myself

onClipEvent(load){
	xspd = 0;
	yspd = .9;
	down = true;
	limity = yspd;
}
onClipEvent(enterFrame){
			_y += yspd;
	if(down == true){
		yspd += .7;
	}
		if(down == false){
		yspd -= .7;
		yspd += 1.4
	}
	if(this.hitTest(_root.platform)){
		yspd = -yspd/1.3;
		down = false;
	}
	Math.round(yspd);
}

Programming languages I know: AS2 and TRS-80 BASIC

BBS Signature

None

Montycarlo

Reply to Post Reply & Quote

Posted at: 10/25/09 01:11 AM

Montycarlo LIGHT LEVEL 19

Sign-Up: 03/14/05

Posts: 737

At 10/25/09 01:08 AM, 7IsUnlucky wrote: You guys are so helpful

Toast gave you exactly what you asked for. It wasn't hard to understand.

At 10/25/09 01:08 AM, 7IsUnlucky wrote: And don't give me any shitty code I can copy and paste; I want pseudo code so I can actually understand wtf I'm doing.

None

7IsUnlucky

Reply to Post Reply & Quote

Posted at: 10/25/09 01:16 AM

7IsUnlucky FAB LEVEL 25

Sign-Up: 06/12/07

Posts: 1,965

At 10/25/09 01:11 AM, Montycarlo wrote: Toast gave you exactly what you asked for. It wasn't hard to understand.

Obviously not, because it didn't work

Programming languages I know: AS2 and TRS-80 BASIC

BBS Signature

None

Montycarlo

Reply to Post Reply & Quote

Posted at: 10/25/09 01:40 AM

Montycarlo LIGHT LEVEL 19

Sign-Up: 03/14/05

Posts: 737

At 10/25/09 01:16 AM, 7IsUnlucky wrote: Obviously not, because it didn't work

You're a fucking retard. You asked us to give you pseudo-code so that you could understand it. Toast gave you it in the form of variable names. It was your job to interpret it. You failed.


None

7IsUnlucky

Reply to Post Reply & Quote

Posted at: 10/25/09 01:46 AM

7IsUnlucky FAB LEVEL 25

Sign-Up: 06/12/07

Posts: 1,965

I replaced everything right and it fucked up

My new code has nothing to do with his and it works

Programming languages I know: AS2 and TRS-80 BASIC

BBS Signature

None

Montycarlo

Reply to Post Reply & Quote

Posted at: 10/25/09 02:15 AM

Montycarlo LIGHT LEVEL 19

Sign-Up: 03/14/05

Posts: 737

At 10/25/09 01:46 AM, 7IsUnlucky wrote: My new code has nothing to do with his and it works

ahhahahahaahhahahahahahahaha
Your Code:

onClipEvent(load){
	xspd = 0;
	yspd = .9;
	down = true;
	limity = yspd;
}
onClipEvent(enterFrame){
			_y += yspd;
	if(down == true){
		yspd += .7;
	}
		if(down == false){
		yspd -= .7;
		yspd += 1.4
	}
	if(this.hitTest(_root.platform)){
		yspd = -yspd/1.3;
		down = false;
	}
	Math.round(yspd);
}

yspd is the velocity.
.7 is the value of accelleration.
yspd = -yspd/1.3;

Resemble anything?

Why are you doing this:

yspd -= .7;
		yspd += 1.4

Why not

yspd += .7

???


None

kizza0

Reply to Post Reply & Quote

Posted at: 10/25/09 02:19 AM

kizza0 NEUTRAL LEVEL 08

Sign-Up: 10/13/07

Posts: 276

Dude, If you actually had a look and tried to understand his post it would have helped you alot. Not to say your doing it wrong but you will find a lot of problems later on if you continue with the way you have done it. It could be a lot simpler.

BOOM HEADSHOT

BBS Signature

None

7IsUnlucky

Reply to Post Reply & Quote

Posted at: 10/25/09 02:19 AM

7IsUnlucky FAB LEVEL 25

Sign-Up: 06/12/07

Posts: 1,965

Need help getting appropriate x axis physics now...

onClipEvent(load){
	xspd = 0;
	yspd = .9;
	down = true;
	bheight = 1
}
onClipEvent(enterFrame){
			_y += yspd;
	if(down == true){
		yspd += .7;
	}
		if(down == false){
		yspd -= .7;
		yspd += 1.4
	}
	if(this.hitTest(_root.platform)){
		yspd = -yspd/1.3;
		down = false;
		bheight *= .8;
	}
	if(bheight <= .25){
		yspd = 0;
	}
	Math.round(yspd);
	_x += xspd;
	Math.round(xspd);
}

Programming languages I know: AS2 and TRS-80 BASIC

BBS Signature

None

Montycarlo

Reply to Post Reply & Quote

Posted at: 10/25/09 02:23 AM

Montycarlo LIGHT LEVEL 19

Sign-Up: 03/14/05

Posts: 737

onClipEvent(load){
	xspd = 0;
	yspd = .9;
	down = true;
	bheight = 1
}
onClipEvent(enterFrame){
	_y += yspd;
	yspd += .7;
	_x += xspd;
	if(this.hitTest(_root.platform)){
		yspd = -yspd/1.3;
		down = false;
		bheight *= .8;
		xspd *= 0.97;
	}
	if(Key.isDown(Key.RIGHT))  xspd ++;
	else if(Key.isDown(Key.LEFT)) xspd --;
	if(bheight <= .25){
		yspd = 0;
	}
}

Use Arrow Keys.


None

7IsUnlucky

Reply to Post Reply & Quote

Posted at: 10/25/09 02:57 AM

7IsUnlucky FAB LEVEL 25

Sign-Up: 06/12/07

Posts: 1,965

Actually nevermind that, I can't find a good way of making the bouncing stop

onClipEvent (load) {
	xspd = 0;
	yspd = .9;
	down = true;
}
onClipEvent (enterFrame) {
	_y += yspd;
	if (down == true) {
		yspd += .7;
	}
	if (down == false) {
		yspd += .7
	}
	if (this.hitTest(_root.platform)) {
		yspd = -yspd/1.3;
		down = false;
	}
	Math.round(yspd);
	_x += xspd;
	Math.round(xspd);
}

Programming languages I know: AS2 and TRS-80 BASIC

BBS Signature

None

Montycarlo

Reply to Post Reply & Quote

Posted at: 10/25/09 02:59 AM

Montycarlo LIGHT LEVEL 19

Sign-Up: 03/14/05

Posts: 737

if (this.hitTest(_root.platform)) {
	yspd = -yspd/1.3;
	if(yspd > - 1.5) yspd = 0;
}

None

7IsUnlucky

Reply to Post Reply & Quote

Posted at: 10/25/09 03:06 AM

7IsUnlucky FAB LEVEL 25

Sign-Up: 06/12/07

Posts: 1,965

That doesn't work that well
at all

Programming languages I know: AS2 and TRS-80 BASIC

BBS Signature

None

Montycarlo

Reply to Post Reply & Quote

Posted at: 10/25/09 03:17 AM

Montycarlo LIGHT LEVEL 19

Sign-Up: 03/14/05

Posts: 737

At 10/25/09 03:06 AM, 7IsUnlucky wrote: That doesn't work that well

The line that I added was the if expression. I don't know exactly how you want it, nor do I care. I gave you a guideline on how it should work. Configure it to suit what you want.


None

Toast

Reply to Post Reply & Quote

Posted at: 10/25/09 10:40 AM

Toast DARK LEVEL 09

Sign-Up: 04/02/05

Posts: 8,924

holy Moses, you're joking right? What you're asking for is something that an 8 year old could figure out. We live on earth, we see gravity every day. Even the 8 year olds know that when they play with their balls, the balls constantly accelerate downwards, not upwards. Now obviously they don't understand those words, and probably neither do you, but they understand how shit works, which you apparently don't. Your code is extremely retarded. You are even more retarded than your code. I really hope you're joking because I'd rather think of you as an immense douchebag than a mentally handicapped person. I could figure this shit out on my own when I was 11 for god's sake.

Knowing that you're 13 years old or younger, I wouldn't be so harsh on you if it weren't for you acting like a total douche on the bbs. Go away.


Goofy

zuperxtreme

Reply to Post Reply & Quote

Posted at: 10/25/09 11:13 AM

zuperxtreme NEUTRAL LEVEL 08

Sign-Up: 01/02/05

Posts: 1,626

lol, making a physics "ENGINE".

;O


None

4urentertainment

Reply to Post Reply & Quote

Posted at: 10/25/09 11:26 AM

4urentertainment NEUTRAL LEVEL 13

Sign-Up: 08/01/08

Posts: 1,594

You're making a physics engine and you can't get past basic gravity?

And why are you rounding the speeds anyway? It would go a lot smoother if you left it normally. Also, this code could be a lot simpler. Just have a variable, acceleration or yspeed that constantly increases. Once it touches the ground, multiply it by negative half or something similar. That would make the ball bounce nicely.

"When you look at yourself from a universal standpoint, something inside always reminds or informs you that there are bigger and better things to worry about. "- Einstein


None

7IsUnlucky

Reply to Post Reply & Quote

Posted at: 10/25/09 11:26 AM

7IsUnlucky FAB LEVEL 25

Sign-Up: 06/12/07

Posts: 1,965

At 10/25/09 10:40 AM, Toast wrote: stupid shit

Wow man, you're a dick
I haven't coded seriously for at least 2 years
All I need help with is cutting off the bouncing

Programming languages I know: AS2 and TRS-80 BASIC

BBS Signature

None

4urentertainment

Reply to Post Reply & Quote

Posted at: 10/25/09 11:32 AM

4urentertainment NEUTRAL LEVEL 13

Sign-Up: 08/01/08

Posts: 1,594

And no wonder the bouncing won't stop, look at this:

if (down == true) {
		yspd += .7;
	}
	if (down == false) {
		yspd += .7
	}

You're saying that if down is true or false, yspeed will increase. Omit that last if statement and you should get something.

"When you look at yourself from a universal standpoint, something inside always reminds or informs you that there are bigger and better things to worry about. "- Einstein


None

7IsUnlucky

Reply to Post Reply & Quote

Posted at: 10/25/09 11:33 AM

7IsUnlucky FAB LEVEL 25

Sign-Up: 06/12/07

Posts: 1,965

At 10/25/09 11:32 AM, 4urentertainment wrote: You're saying that if down is true or false, yspeed will increase. Omit that last if statement and you should get something.

Oh lmao, thanks

Programming languages I know: AS2 and TRS-80 BASIC

BBS Signature

None

7IsUnlucky

Reply to Post Reply & Quote

Posted at: 10/25/09 02:27 PM

7IsUnlucky FAB LEVEL 25

Sign-Up: 06/12/07

Posts: 1,965

Is this code better written guys?

onClipEvent (load) {
	xspd = -5;
	yspd = .9;
	down = 1;
}
onClipEvent (enterFrame) {
	_y += yspd;
	yspd += .7;
	if (this.hitTest(_root.platform)) {
		yspd = -yspd/1.3;
	}
	_x += xspd;
	if(xspd > 0){
		xspd -= .05;
	}
	if(xspd < 0){
		xspd += .05;
	}
	if(yspd > 40){
		yspd = 40;
	}
	if(this.hitTest(_root.wall)){
		xspd = -xspd;
	}
}

I still can't fix the bouncing, though

Programming languages I know: AS2 and TRS-80 BASIC

BBS Signature

None

ThePeasant

Reply to Post Reply & Quote

Posted at: 10/25/09 05:10 PM

ThePeasant NEUTRAL LEVEL 08

Sign-Up: 08/30/07

Posts: 448

Here is the basic physics you need to make what you're working on:

v = a*t
d = v*t

where 'v' is Velocity, 'a' is Acceleration, 'd' is Distance and 't' is time.

We'll be using a 't' value of 1 itineration, so we won't need to worry about it.

This means every frame you want to add your 'acceleration' value to 'velocity' and your 'velocity' to 'distance'. This you've figured out on your own, it seems. For efficiency, however, you should put your a value as a variable so you don't have to look through your code to find it.

There is a constant downwards acceleration called 'gravity'. This means every object should, at all times, be accelerating downwards at your gravity constant, no matter where it is or whatever other forces are acting on it.

Now, whenever you have one object hitting another or hitting a wall you have what's called a 'collision'. You have 'elastic collisions', where the objects bounce away and 'inelastic collisions', where the objects don't bounce. There's no such thing as a 100% elastic collision; every time two objects collide some of the momentum is lost.

Every pairing of objects should have an elasticity constant between 0 and 1, 0 being totally inelastic and 1 being totally elastic. Then, you multiply the current velocity by the negative of the elasticity value.

eg. if Elast = 0.3 you would do:

if (collision) v *= -0.3;

Every time it bounces, the velocity is reduced.

In total, your code should be as follows:

CONSTANTS:

gravity, elast

VARIABLES

vX, vY, x, y

EVERY FRAME

vY+= gravity;
x+=vX; y+=vY;

if (ball collides with ground) vY*= (0-elast)
if (ball collides with wall) vX*= (0-elast)

And that's it!


None

7IsUnlucky

Reply to Post Reply & Quote

Posted at: 10/25/09 05:21 PM

7IsUnlucky FAB LEVEL 25

Sign-Up: 06/12/07

Posts: 1,965

That really didn't help with the infinite bouncing problem...

Programming languages I know: AS2 and TRS-80 BASIC

BBS Signature

None

ProfessorFlash

Reply to Post Reply & Quote

Posted at: 10/25/09 05:35 PM

ProfessorFlash DARK LEVEL 24

Sign-Up: 10/06/07

Posts: 809

Good post Peasant.

At 10/25/09 05:21 PM, 7IsUnlucky wrote: That really didn't help with the infinite bouncing problem...

Probably because you are a dumb 13-year-old. Stay in school and try again in a few years. I'm surprised you've had this much help in this thread. With your attitude "that didn't work, give me another code", you deserve a slap, not help. Some people in this thread are saints.


None

knugen

Reply to Post Reply & Quote

Posted at: 10/25/09 05:43 PM

knugen LIGHT LEVEL 35

Sign-Up: 02/07/05

Posts: 4,690

At 10/25/09 05:21 PM, 7IsUnlucky wrote: That really didn't help with the infinite bouncing problem...

It was the most helpful post I've seen in a good while. He basically told you everything one needs to know when making simple platformers, if you can't see that how are you going to make a whole physics engine?


None

zrb

Reply to Post Reply & Quote

Posted at: 10/25/09 05:43 PM

zrb LIGHT LEVEL 11

Sign-Up: 08/08/06

Posts: 4,548

At 10/25/09 05:35 PM, ProfessorFlash wrote: Probably because you are a dumb 13-year-old. Stay in school and try again in a few years. I'm surprised you've had this much help in this thread. With your attitude "that didn't work, give me another code", you deserve a slap, not help. Some people in this thread are saints.

I totally agree, these codes most likely won't help unless you understand the concept of physics. So stop wasting people's time and instead of whining for more codes you won't understand, grab some physics book and LEARN orjust wait a few years in school. Move on to something you can understand.

School Sux ! || As :Main || As3: Main || Animation: Main || Flash Tutorials ||

BBS Signature

Happy

doctormario

Reply to Post Reply & Quote

Posted at: 10/25/09 05:48 PM

doctormario NEUTRAL LEVEL 07

Sign-Up: 01/30/09

Posts: 685

Not to demean anyone, but I'm sort of surprised this thread is still going.


None

ThePeasant

Reply to Post Reply & Quote

Posted at: 10/25/09 05:51 PM

ThePeasant NEUTRAL LEVEL 08

Sign-Up: 08/30/07

Posts: 448

At 10/25/09 05:21 PM, 7IsUnlucky wrote: That really didn't help with the infinite bouncing problem...

Oh! I forgot... you should also have:

const minValue=1;

if (Math.abs(vY)<minValue) vY=0;
if (Math.abs(vX)<minValue) vX=0;

with that in there, does it work?


All times are Eastern Standard Time (GMT -5) | Current Time: 08:35 PM

<< Back

This topic is 2 pages long. [ 1 | 2 ]

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!