Raised This Month: $ Target: $400
 0% 

please help with some Geometry and calculations.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Podunk
Senior Member
Join Date: Nov 2005
Location: Florida
Old 06-23-2007 , 10:13   please help with some Geometry and calculations.
Reply With Quote #1

I'm trying to figure out a way to calculate an angle.

The angle is derived from 2 sets 2 dimensional vector coordinates



I want to draw a line from point A to point B


example:
[point A]
(X,Y)
|
|
|
[point B]
(X,Y)


the angle origin is [horizontal left]:


<---- 0 degrees
^
|
| 90 degrees

---> 180 degrees

|
| 270 degrees
V

In the example the angle would be 270 degrees.


My second problem is a programming one.

I'm working with a 2D plane, and I have 32 objects which move freely around on the plane.

The program has a "ticker" that fires about 12 times a second. - I'll use this to move the objects around each tick.
I need to figure out how to smoothly move all of the objects to their destinations each "tick".

Each object has individual destinations and speeds of travel which makes this fuzzy
__________________
Check out my website at http://matthewmiller.info
Podunk is offline
Send a message via MSN to Podunk
TheNewt
Donor
Join Date: Jun 2006
Location: Where I live.
Old 06-23-2007 , 12:06   Re: please help with some Geometry and calculations.
Reply With Quote #2

so create arrays like this
objLoc[32][2]
objVel[32][2]

The first array will store the object's location and the second will store how quickly the objects move in what direction.

so object 1's X location would be objLoc[0][0] and the same object's Y location is objLoc[0][1].

I'd probably write a loop that would just tell each object to update
Code:
public static void main(String[] args) {
	loop();
}

loop() {
	while(true) {
		//You could write something here to change the object's velocity.
		update();
	}
}

update() {
	for(int i = 0; i<32; i++) {
		objLoc[i][0] += objVel[i][0];
		objLoc[i][1] += objVel[i][1];
	}
}
That'd be one way to
__________________
Quote:
toe3_ left the chat room. (G-lined (AUTO Excessive connections from a single host.))

Last edited by TheNewt; 06-23-2007 at 12:09.
TheNewt is offline
_Master_
Senior Member
Join Date: Dec 2006
Old 06-23-2007 , 12:17   Re: please help with some Geometry and calculations.
Reply With Quote #3

And now for the bomb: COLLISIONS.
_Master_ is offline
TheNewt
Donor
Join Date: Jun 2006
Location: Where I live.
Old 06-23-2007 , 12:21   Re: please help with some Geometry and calculations.
Reply With Quote #4

LOL
__________________
Quote:
toe3_ left the chat room. (G-lined (AUTO Excessive connections from a single host.))
TheNewt is offline
Podunk
Senior Member
Join Date: Nov 2005
Location: Florida
Old 06-24-2007 , 02:49   Re: please help with some Geometry and calculations.
Reply With Quote #5

Thanks Mystic, but that would only work if the ticker was exactly 12 times a second. The ticker is variable and could fire 1-12 times a second so I have to use time to move the objects.
__________________
Check out my website at http://matthewmiller.info
Podunk is offline
Send a message via MSN to Podunk
OneEyed
AMX Mod X Beta Tester
Join Date: Jun 2005
Old 06-26-2007 , 21:16   Re: please help with some Geometry and calculations.
Reply With Quote #6

First Problem:

Length = sqrt( x*x + y*y )
Normal = ( x/length , y/length )
DOT Product = (x1*x2 + y1*y2) //Use normalized points

Angle Between 2 Vectors = acos( point1 DOT point2 )

acos can be arccos, cosarc, icos, invcos, cosinv, whatever the language your using made it.

In PAWN code.
Code:
new Float:a[2] = { 1.0, 5.0 }; new Float:b[2] = { -3.0, 2.0 }; new Float:lenA = floatsqrt( (a[0]*a[0]) + (a[1]*a[1]) ); new Float:lenB = floatsqrt( (b[0]*b[0]) + (b[1]*b[1]) ); a[0] /= lenA; a[1] /= lenA; b[0] /= lenB; b[1] /= lenB; new Float:dotprod = ( (a[0]*b[0]) + (a[1]*b[1]) ); //If you want radians change "degrees" to "radians". new Float:angle = floatacos( dotprod, degrees);

Second Problem:
To move gradually along a line, you'll need to adjust your direction (IE. Velocity) to size up to the distance to be traveled in one second. Then to make your object gradually move across the direction. Divide it up by how many ticks you run a second. The higher # of ticks the smoother it'll look.

Pseudo Code Example:
Code:
//Runs 12 times a second. GameFrame() {     float rate = 12.0;     Vector velocity;     velocity.x = 3.0;     velocity.y = 5.0;     ObjectOrigin.x += velocity.x / rate;     ObjectOrigin.y += velocity.y / rate; }

That there will move your object's origin up 3 units and across 5 units every second.
__________________

Last edited by OneEyed; 06-26-2007 at 22:27.
OneEyed is offline
_Master_
Senior Member
Join Date: Dec 2006
Old 06-27-2007 , 17:53   Re: please help with some Geometry and calculations.
Reply With Quote #7

Unless your objects are insolid, you seem to be missing the point. Making an object "move" in real-phisics is the easy part. How to avoid objects is hell, and I'm fairly sure a little AI is added into it.

As a side-note: check "line" algorithms (from C/C++/JAVA/etc.) for incremental x, y calculations. You can assume x,y to be the next step and the increment as velocity. WAY faster to compute.

Last edited by _Master_; 06-27-2007 at 17:59.
_Master_ is offline
Podunk
Senior Member
Join Date: Nov 2005
Location: Florida
Old 06-30-2007 , 07:21   Re: please help with some Geometry and calculations.
Reply With Quote #8

very nice info thanks a lot guys!
__________________
Check out my website at http://matthewmiller.info
Podunk is offline
Send a message via MSN to Podunk
Podunk
Senior Member
Join Date: Nov 2005
Location: Florida
Old 06-30-2007 , 07:35   Re: please help with some Geometry and calculations.
Reply With Quote #9

Quote:
Originally Posted by OneEyed View Post
First Problem:

Length = sqrt( x*x + y*y )
Normal = ( x/length , y/length )
DOT Product = (x1*x2 + y1*y2) //Use normalized points

Angle Between 2 Vectors = acos( point1 DOT point2 )
Say I wanted to move from

1,1
to
13,15

Length = sqrt(1*1+13*15) = 14
Normal1 = (1*14,1*14) = (14,14)
Normal2 = (13*14,15*14) = (182,210)
DOT Product = (1*13+1*15) = 28
angle = acos((14,14)28(182,210)) = what?

I totally get lost around Normal1.
__________________
Check out my website at http://matthewmiller.info
Podunk is offline
Send a message via MSN to Podunk
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 17:37.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode