Solved

Making Objects Ricochet

Posted on 2011-09-02
18
447 Views
Last Modified: 2013-11-11
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
Comment
Question by:Mr_Fulano
  • 10
  • 8
18 Comments
 
LVL 29

Expert Comment

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

Author Comment

by:Mr_Fulano
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

by:dgofman
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

by:Mr_Fulano
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

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

Author Comment

by:Mr_Fulano
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

by:dgofman
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

by:dgofman
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

by:Mr_Fulano
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?"

Any thoughts about that...?
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 29

Accepted Solution

by:
dgofman earned 500 total points
ID: 36518022
0
 
LVL 29

Expert Comment

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

Author Comment

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

0
 

Author Comment

by:Mr_Fulano
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

by:dgofman
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

by:Mr_Fulano
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

by:Mr_Fulano
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

by:Mr_Fulano
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

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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Recently, I was asked to recommend a tracking system to be implemented on a clients website. As the entire site was built on flash, my first thought was to suggest custom built tracking system. However, our company at that point of time didn't h…
In my long career of working as an actionscript developer, I had spent sleepless night often working hard to solve some small problems which actually took a lot of my development time; later found out the solutions to be a line or two. Here are s…
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now