Forum Topic: Help with bouncing ball

(125 views • 9 replies)

This topic is 1 page long.

<< < > >>
Sad

faxmunky

Reply To Post Reply & Quote

Posted at: 1/30/09 06:56 AM

faxmunky DARK LEVEL 04

Sign-Up: 12/03/08

Posts: 257

Hello,

I'm creating a game and I'm planning on adding in throwable items that incorporate gravity (Like grenades).

I've come up with some code here, it collides with the bottem of the stage which is 400 on the y axis. But I've decided that I do not want it to bounce from the bottom of the stage, I want it to bounce from the scenery ground. This ground may not be perfectly flat and may have hills or obstacles.

Here is my code.

onClipEvent(load){
	var ground:Number = 400;
        var yVel:Number = 0;
	var gravity:Number = 2;
	var radius:Number = this._height/2; //Not 100% sure
	var bounce:Number = -.85;
	var xVel:Number = 15;
	var xDec:Number = 0.3;
}

onClipEvent(enterFrame){
	this._x += xVel;
	if(xVel>0){
		xVel = xVel-xDec;
	}
}

onClipEvent(enterFrame){
		var y:Number = this._y
}

onClipEvent(enterFrame){
	yVel += gravity;
	this._y += yVel;
	if(this._y+radius>ground){
		yVel *= bounce;
		this._y = ground-radius;
	}
}
on (press) {
	yVel = 0
	gravity = 0
	startDrag(_root.grenade);
}

on (release) {
	yVel = 0
	gravity = 1
	stopDrag();
}

So, what I am trying to do is remove the entire 'ground' variable which is a y co-ordinate and replace it with a hitTest for _root.ground. I'm just feeling tired and I'm not sure exactly how I would do this.

All comments and help are greatly appreciated.

Thanks,
Faxmunky

BBS Signature

None

OctoFlash

Reply To Post Reply & Quote

Posted at: 1/30/09 06:59 AM

OctoFlash DARK LEVEL 17

Sign-Up: 12/02/06

Posts: 839

okay, first of all: is this your code? or did you just copy and paste it? i mean: do you understand what you are showing use here? because if you want to work with this kind of codes you gotta know how to use them..

BLAM DAMN SPAM. That rhymes! So what do we learn?
the right answer for the most bbs-posts: THE FREAKIN' COLLAB RULES!!!

BBS Signature

None

faxmunky

Reply To Post Reply & Quote

Posted at: 1/30/09 07:08 AM

faxmunky DARK LEVEL 04

Sign-Up: 12/03/08

Posts: 257

At 1/30/09 06:59 AM, OctoFlash wrote: okay, first of all: is this your code? or did you just copy and paste it? i mean: do you understand what you are showing use here? because if you want to work with this kind of codes you gotta know how to use them..

I wrote this code myself. I did not copy and paste. I understand it.

BBS Signature

None

BillysProgrammer

Reply To Post Reply & Quote

Posted at: 1/30/09 07:42 AM

BillysProgrammer LIGHT LEVEL 15

Sign-Up: 09/17/08

Posts: 1,870

If you even had a thought to program, we would see more posts on here lol. Also, you would know not to use more then 1 clipEvent, enterFrame etc.

Motto: Continue with what your doing and you will succeed!
Newgrounds: Where all us freaks go.

BBS Signature

None

Munz

Reply To Post Reply & Quote

Posted at: 1/30/09 07:44 AM

Munz NEUTRAL LEVEL 11

Sign-Up: 01/20/08

Posts: 448

no this guys ligit i know him

Fluidanims is awesome

BBS Signature

None

BillysProgrammer

Reply To Post Reply & Quote

Posted at: 1/30/09 07:46 AM

BillysProgrammer LIGHT LEVEL 15

Sign-Up: 09/17/08

Posts: 1,870

Not really trying to imply that he's lying. Because personally, I am not sure and I do not know him. If you know he's legit, then there's no problem here.

Motto: Continue with what your doing and you will succeed!
Newgrounds: Where all us freaks go.

BBS Signature

None

Nuttro

Reply To Post Reply & Quote

Posted at: 1/30/09 08:36 AM

Nuttro LIGHT LEVEL 22

Sign-Up: 04/26/07

Posts: 850

Well personally I think it was copied and pasted the way it's set out


None

ThePeasant

Reply To Post Reply & Quote

Posted at: 1/30/09 08:43 AM

ThePeasant NEUTRAL LEVEL 08

Sign-Up: 08/30/07

Posts: 446

doing collision testing for circles is apparently complicated. I'm learning about hittests now actually, and from what I've gathered, making a square bounding box around your object and using that to test with is the easiest way to go.

Here's how to do collision testing, step by step:

1) determine the direction that an object is travelling

2) project an image in front of the object using whatever bounding points you have in that direction (for a box it is 2 or 3. For a circle it can be 20 or more... unless you use Vectors, but testing between circles and oddly shaped terrain using vectors is very confusing)

3) test to see if the image overlaps whatever you are testing against.

4) if so, react to it! In the case of a bouncing object, create a virtual 'flat plane' at the point where the object comes in contact with the ground and calculate a new trajectory as if it bounced off of that flat plane.


None

faxmunky

Reply To Post Reply & Quote

Posted at: 1/30/09 04:46 PM

faxmunky DARK LEVEL 04

Sign-Up: 12/03/08

Posts: 257

Firstly, I wrote this code myself full stop. I don't give a shit what you think.

At 1/30/09 07:44 AM, Munz wrote: no this guys ligit i know him

Thanks mate,

At 1/30/09 08:43 AM, ThePeasant wrote:

:An explanation that is too high a % for this post.

Thanks, that works great.

BBS Signature

None

faxmunky

Reply To Post Reply & Quote

Posted at: 1/30/09 04:52 PM

faxmunky DARK LEVEL 04

Sign-Up: 12/03/08

Posts: 257

At 1/30/09 07:42 AM, BillysProgrammer wrote: If you even had a thought to program, we would see more posts on here lol. Also, you would know not to use more then 1 clipEvent, enterFrame etc.

Sorry for double post.

I use more than one clipEvent, enterFrame etc. because It's a mess if I don't. It organises it better and that's just a personal thing. I've also read that it does not slow it down. This is because when you run it, it automatically groups them altogether.

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 09:52 PM

<< Back

This topic is 1 page long.

<< < > >>
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!