Solved

# Making Objects Ricochet

Posted on 2011-09-02
455 Views
Hi, I have two object that I'd like to have ricochet when they collide. I can identify when they collide using the "object1.hitTestObject(object2)" command, but how would I make them each go in opposite directions?

Right now I'm using the following to make them move in a sloped path:

object1.x = slopeX;
and
object1.y = slopeY;

Clearly, I can invert the slopes, but there has to be a more elegant approach (at least I would think...).

So, I was wondering if there is a better way to tell the object to travel in a path that is 180 degrees from its current path or in essence ricochet.

Thanks,
Fulano
0
Question by:Mr_Fulano
• 10
• 8

LVL 29

Expert Comment

ID: 36478357
Use nagative sign should work from start point
0

Author Comment

ID: 36479581
I tried that and it works, but it has some glitches...I was looking for an example of how someone has done it in the past. There has to be a better way than just using the negative.

I can't imagine this is a first, so someone has to have coded this behavior before. I just need to find where that is...
0

LVL 29

Expert Comment

ID: 36479586
I am not sure you will find other magic except using Math calculation. All games using 60% basic mathematical formulas such as Math.abs/cos/cin, mod, negative etc.  I am fine to wait if someone will give you other solutions
0

Author Comment

ID: 36481147
Hi Dgofman, I understand what you're saying...I was hoping someone had already figured it out and perhaps there was some sort of function already in place that I could use.

That said, I've been thinking about it and this is what I'm going to try to do...but first, lets analyse the problem.

The problem is not making an object ricochet at 180 degrees from its original path when it collides with another object. The problem is making multiple objects ricochet all at once in different directions. The issue here is that you have to keep track of all the slopes individually!

Lets assume for our example that we have 10 instances of the redBall on the stage, all moving around in different directions and all hitting the edges of the stage and bouncing off of that edges back into the stage. I can do that much thus far.

In this example I had to assign multiple instances of the "x" and the "y" property for each instance, hence "individual slopes." In other words, the "x" value for redBall1 is not the same as the "x" value for redball2.

So, based on that, you have an instance with its individual "x" and "y" properties, and now that instance is going to collide with another instance of the RedBall Class, that also has individual values for its "x" and "y" properties.

I've been thinking of designing a Class that has the instance name, the "x" and the "y" parameters built into that Class. So, when my two objects collide, I can simply say:

instance1.x1 = - instance1.x1;
instance1.y1 = - instance1.y1;

and

instance2.x2 = - instance2.x2;
instance2.y2 = - instance2.y2;

In this way, I can set my two instances in completely opposite paths, with more specificity and they will be easier for address in the code.

Any thoughts...?

Fulano
0

LVL 29

Expert Comment

ID: 36482407
Yes you can, but please check my sample how you can do without class declaration
ball.fla
0

Author Comment

ID: 36508558
Hi Dgofman, I've been trying to understand what you did and how your code works in terms of a collision and a bounce back, but am having a bit for trouble seeing your logic.

Can you step through your code in the ball.fla file and explain your logic?

Thanks,
Fulano
0

LVL 29

Expert Comment

ID: 36515325
Surprise, you cannot understand so simple logic.

var step:Number;
var pos:Number = 0;
var offset:Number = redBall1.width;

var x_ball1:Number = 0;
var x_ball2:Number = stage.stageWidth - offset;

function moveBall(){
if(pos >= stage.stageWidth - offset){
step = -5;
pos = stage.stageWidth - offset;
}else if(pos <= 0){
step = 5;
pos = 0;
}
pos += step;

redBall1.x = x_ball1 + pos;
redBall2.x = x_ball2 + -pos;
}

setInterval(moveBall, 10);
0

LVL 29

Expert Comment

ID: 36515330
We have two balls one starts from 0 another from  stage.width
Now Iâ€™m incrementing position by positive 5 pixels and I when position reached stage.width I changing to negative 5 pixels

So, now with time interval 10 milliseconds I changing x coordinate for two balls

redBall1.x = x_ball1 + pos;
redBall2.x = x_ball2 + -pos;

That it itâ€™s so simple
0

Author Comment

ID: 36515404
OK...now I see what you're doing. However, keep in mind I'm a beginner at this, so simple to you may not always be simple to me...

That said, I understand why I couldn't understand what you were trying to do (of that makes sense). You're not actually coding for a collision, you're actually predetermining their path, their intersection and their bounce back direction...that's why I couldn't figure out how the heck you had identified a collision.

This is not what I'm trying to figure out...

First, I instantiate the instances of the balls in my code. You actually placed them on the stage. Also, I will never know where the two balls are going to collide, because I am not predetermining a path.

This is not identifying and reacting to a collision, this a pre programmed path.

Sure its easy, but it does not address my original question, whcih was:

"I can identify when they collide using the "object1.hitTestObject(object2)" command, but how would I make them each go in opposite directions?"

0

LVL 29

Accepted Solution

dgofman earned 500 total points
ID: 36518022
0

LVL 29

Expert Comment

ID: 36518081
This tutorial is very good for ActionScript 2.
0

Author Comment

ID: 36519458
I'm doing AS3, which from what I understand is completely different code. How does that affect the tutorials?

0

Author Comment

ID: 36519477
Also, specifically which one of these videos address my question regarding collisions and bounce back? There are a lot of videos here and if you think I should watch them to answer my question, which video made you think that?
0

LVL 29

Expert Comment

ID: 36519519
Is not big problem migration to AS3 if you know concept how collision working in AS2.
Watch all videos is teaching how to write code by step to step.
0

Author Comment

ID: 36520772
Dgofman, I appreciate you sharing the link with me and yes, it will help in general edification of how to write code. However, that still does not answer my specific question.

I am asking for help with how to make objects ricochet away from each other when they collide. I know that I can learn from watching all the videos, but that is not what I'm asking about and not the solution the question deserves.

If you do not know how its done, then that's fine. I'm OK with that, but don't drag this question out providing me answers that are not specific.

I don't have a problem awarding points to those that try to help, but I don't consider suggesting that I watch 2 dozen videos or more, help with a specific question.

0

Author Comment

ID: 36520780
Just to be clear...there are nearly 90 videos on this site. Again, a good opportunity for learning, but it is not specific to my question, unless you can suggest one or two specific videos that address my question - specifically.
0

Author Comment

ID: 36520867
Dgofman, I'm just going to close this question out...I'll award you the points to save myself the time.
0

Author Closing Comment

ID: 36520869
Not a specific solution and had to dig through the answer far too much.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

Here are some practices and techniques that can be adopted into your Flash/Flex application development process. Note: Not all "performance tips" provide an immediately-recognizable benefit.   This article does not include timing validation data,â€¦
While working over numerous projects I often had the requirement for doing a screen capture in AS3.0. Unfortunately I found no "ready made" solutions in google search that suited my requirements. But I did come across some great resources which helpâ€¦
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): â€¦
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.