Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (521)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (589)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Pong Physics  (Read 2082 times)
0 Members and 1 Guest are viewing this topic.
Offline Del-ONE

Senior Newbie





« Posted 2009-02-01 07:56:23 »

Hi, I'm looking for verification on the math I'm using for simple Pong-like physics for arbitrary convex polygons (i.e. collisions always involve a static structure).  So here's my solution:

1. Two bodies collide one of which has a velocity vector and create an intersection
2. The normal to the surface is found by finding the angle from the intersection center to the center of the moving object
3. The velocity vector is reversed by rotating 180 degrees
4. The new velocity vector is flipped over normal angle by adding the angle between the velocity vector and the normal to the normal.

Like this:



Here's my code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
      Rectangle me  = shape.getBounds();
      Rectangle him = ob.getShape().getBounds();
      Rectangle col = me.intersection(him);
      Point meCent = new Point((int)(me.getX()+me.getWidth()/2),(int)(me.getY()+me.getHeight()/2));
      Point colCent = new Point((int)(col.getX()+col.getWidth()/2),(int)(col.getY()+col.getHeight()/2));
      double normAngle = Math.atan2(meCent.getY()-colCent.getY(),meCent.getX()-colCent.getX());
      double velAngle = Math.atan2(dy,dx);
      double newAngle = normAngle+(normAngle-(velAngle-Math.PI));
     
      //I know this is not the best way to push out of the collision
      move(-5*Math.cos(velAngle), -5*Math.sin(velAngle));

      dx = 50*Math.cos(newAngle);
      dy = 50*Math.sin(newAngle);


I am fairly sure this is conceptually correct, but it never hurts to be sure, besides I got a few questions about it:
-Is this the most efficient way of doing things? (Specifically getting intersection and angles)
-Am I guaranteed that the normal angle will be correct (Assuming I never sink into a collision past the mid-point)
-Am I right in thinking that adding in force, friction, and collisions between 2 moving bodies would not be far off from this?
-Is there a way to easily/effeciently get perfect intersections rather than rectangular approximations

Thanks for the time
Offline Karmington

Senior Devvie


Medals: 1
Projects: 1


Co-op Freak


« Reply #1 - Posted 2009-04-12 01:18:26 »

1. one basics yes all well
2- if you calculate true path accounting speed > paddle width, ok
3 physics more, curves whatever yes indeed
4 rect collision is fastest. if you want more, be it vector or pixel, it will cost you. but these days what is a few
computations here or there?

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xFryIx (57 views)
2014-11-13 12:34:49

digdugdiggy (35 views)
2014-11-12 21:11:50

digdugdiggy (29 views)
2014-11-12 21:10:15

digdugdiggy (23 views)
2014-11-12 21:09:33

kovacsa (46 views)
2014-11-07 19:57:14

TehJavaDev (50 views)
2014-11-03 22:04:50

BurntPizza (50 views)
2014-11-03 18:54:52

moogie (65 views)
2014-11-03 06:22:04

CopyableCougar4 (63 views)
2014-11-01 23:36:41

DarkCart (148 views)
2014-11-01 14:51:03
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!