Jump to content
  • Advertisement
Sign in to follow this  
brody

Java object interaction

This topic is 4615 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to make an applet the consists of two rectangles. One rectangle is stationary and the other one follows the mouse. I've found lots of posts that explain collision dectection, but I haven't found any help on what to do after a collision is dectected. I want to allow the rectangle that tracks the mouse to be able to push the stationary rectangle around the screen. Right now I just have four rudimentary if statements that try and figure out what side the stationary rectangle is being pushed from and then update it's position; however this is very glitchy. If anyone can point me to a good beginners tutorial, or post some suggestions that would be great.

Share this post


Link to post
Share on other sites
Advertisement
Hey, I talked to you on AIM just a few hours ago and showed you some code, but it was defective. I've got it working now, but I can't remember your AIM so I'll just post it here. This is the physics part of the program which is what I beleive you were having the most trouble with. In my array the second index is which variable on a box they are as follows : 0>x center, 1>y center, 2> xvelocity, 3 > y velocity. r is the radius of the boxes. This code makes the boxes bounce off of each other. If you just want to push the boxes then you can take out the velocity part of the collision response(or the velocity out of the program altogether). You can AIM me with questions..


public void actionPerformed(ActionEvent e )//used for the timer
{
double a,b,c,d ;

for(int k=0;k<box.length;k++){
box[k][0]+=box[k][2] ;
box[k][1]+=box[k][3] ;

for(int j=0;j<k;j++){
if(box[j][0]>box[k][0]-(2*r) && box[j][0]<box[k][0]+(2*r) // if colliding
&& box[j][1]>box[k][1]-(2*r) && box[j][1]<box[k][1]+(2*r) ){
if(Math.abs(box[j][0]-box[k][0])>Math.abs(box[j][1]-box[k][1])){
a = (box[k][0]-box[j][0]) ;
box[k][0]-=Math.abs(a)/a*(Math.abs(a)-2*r)*Math.abs(box[j][2])/(Math.abs(box[j][2])+Math.abs(box[k][2])) ;
box[j][0]+=Math.abs(a)/a*(Math.abs(a)-2*r)*Math.abs(box[k][2])/(Math.abs(box[j][2])+Math.abs(box[k][2])) ;
c = box[k][2] ;
box[k][2]=box[j][2] ;
box[j][2]=c ;
}else{
a = (box[k][1]-box[j][1]) ;
box[k][1]-=Math.abs(a)/a*(Math.abs(a)-2*r)*Math.abs(box[j][3])/(Math.abs(box[j][3])+Math.abs(box[k][3])) ;
box[j][1]+=Math.abs(a)/a*(Math.abs(a)-2*r)*Math.abs(box[k][3])/(Math.abs(box[j][3])+Math.abs(box[k][3])) ;
c = box[k][3] ;
box[k][3]=box[j][3] ;
box[j][3]=c ;
}
}
}


}

repaint();
}

Share this post


Link to post
Share on other sites
I just tried your new code, and it works much better now, thanks. My aim is votecdude If your wondering.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!