Home   Help Search Login Register  

Author Topic: help with maths...  (Read 715 times)

0 Members and 1 Guest are viewing this topic.

Offline bedges

  • Administrator
  • *****
    • OFPEC The Editing Center
help with maths...
« on: 18 Oct 2005, 22:52:24 »
this is embarrassing, but i can't figure it out.

given the player's position and direction, i want to be able to place a predetermined number of objects in front of the player. depending on the number of objects, i want them to be arranged in a semi-circular arc.

for example, draw an imaginary line from the player in the direction he's facing. if there is one object, i want it on that line. if there are two objects, i want them on either side of that line. if there are three objects, one will be on that line, the other two one either side, and so on.

i can't figure out how to approach this. i can imagine the effect i want, but not how to get at it using pure maths.

help  :-\

Offline macguba

  • Former Staff
  • ****
    • macguba's operation flashpoint page
Re:help with maths...
« Reply #1 on: 18 Oct 2005, 23:05:30 »
1. Figure out how to place one object at a predetermined angle to the player.   In other words brush up on your trigonometry.

2. Decide what is the range of angles you want for your multiple objects.   Let's say its up to 90 degrees either side.

3. Write a script that:
- puts the objects in an array
- counts them
- figures out if the count is odd or even and goes to the appropriate bit in the script
- places the first object, calculating the angle it makes with the player by dividing the maximum (in this case 90) by the count, adjusted of course for odd, even, and the fact that half the count is going on each side
- places the remaining objects, either alternatly left and right, or the first half to the left, or whatever you want

There is no maths other than arithmetic and trigonometry.   Possibly the hard part will be the logic of how the arrangement of places is handled.   You could do it crudely with "if _count=1: goto "calc1", etc.    

However the right way is something along the lines of  "if count is even, then _angle=90/2*_count", followed by "_placeAngle = _angle * _i".    Don't quote me.

Does that rather bad sketch help help point you in the right direction?
« Last Edit: 18 Oct 2005, 23:10:23 by macguba »
Plenty of reviewed ArmA missions for you to play

Offline Raptorsaurus

  • Editors Depot Staff
  • *****
Re:help with maths...
« Reply #2 on: 19 Oct 2005, 01:06:18 »
bedges,

This function might help.
« Last Edit: 19 Oct 2005, 01:07:32 by Raptorsaurus »

Offline ACF

  • Members
  • *
  • Llama?? Ain't that French for tanks?
Re:help with maths...
« Reply #3 on: 19 Oct 2005, 01:49:42 »
This might too...

Code: [Select]
_N = 5
_D = 20
_Px = (GetPos player) Select 0
_Py = (GetPos player) Select 1
_i = 1

#loop
_dangle = GetDir player - 90 + ((_i * 180)/(_N + 1))

_Ax = _Px + _D * Sin _dangle
_Ay = _Py + _D * Cos _dangle

_temp = "chairx" CamCreate [_Ax,_Ay,0]

_i = _i + 1
?(_N >= _i): Goto "loop"

Exit

; places _N objects in a semicircle radius _D
; from the player's left (-90 degees)
; at an angular interval of 180/(_N+1) so:
; _N  int  rel ang
;  1   90   0
;  2   60   -30,+30
;  3   45   -45,0,+45
;  4   36   -54,-18,+18,+54
;  5   30   -60,-30,0,+30,+60

Note that it will not place objects directly left and right of the player but they do tend to those positions as the number of objects increases.  For a different distribution it's the _dangle algorithm you need to tweak, the trig bit (_Ax and _Ay) won't need to change.

Offline Triggerhappy

  • Contributing Member
  • **
  • Llama, it's what's for dinner.
Re:help with maths...
« Reply #4 on: 19 Oct 2005, 03:04:21 »
Code: [Select]
;radius of circle
_d = 5
;angle difference between weapon placement
_anglediff = 2
;pass list of weapons to script
_list = _this
_a = (getdir player)
_p = (getpos player)
_c = count _list
_o = _c/2
_o = _o - (_o mod 1)
;odd or even? if even adjust the angle
?_o == _c/2:_a = _a - (_anglediff/2 )
;furthest angle
_a = _a - (_o * _anglediff)
_i = 0

#loop
_weapon = _list select _i
_pos = [((_p select 0) + (_d * cos _a)),((_p select 1) + (_d * sin _a)),_p select 2]
;insert creating whatnot here....
?_i = _c:exit
_i = _i + 1
_a = _a + _anglediff
goto "loop"

tadaa!

for some reason i feel like i got my sines and cosines mixed or some other dumb mistake, and therefore not garunteed

Offline Raptorsaurus

  • Editors Depot Staff
  • *****
Re:help with maths...
« Reply #5 on: 19 Oct 2005, 03:59:20 »
This one is tested and guaranteed.  It uses the function I attached earlier.  Attached is a demo mission folder including a picture of what you get when you want 10 objects starting at minimum arc of -95 deg. to left of player and ending +95 deg. to right of player at 15 m distance.  For the objects I used camp fires (since they show up easily).

Code: [Select]
_refobj = _this select 0
_num = _this select 1
_minarc =_this select 2
_maxarc = _this select 3
_dis = _this select 4

_arcinc = abs(_maxarc - _minarc) / _num

_dir = _minarc

#loop
_pos = ([_refobj, 0, _dir, _dis] call AngDirDisOff) select 0
_fire = "fire" createVehicle [0,0,0]
_fire inflame true
_fire setPos _pos
_cnt = _cnt + 1
_dir = _dir + _arcinc
~.001
? _dir <= _maxarc : goto "loop"

exit

Offline Tyger

  • Former Staff
  • ****
  • I was at OFPEC when it still had dirt floors...
    • OFPEC
Re:help with maths...
« Reply #6 on: 19 Oct 2005, 04:04:24 »
Seems like you have quite a few to choose from. You might also want to check out toadlife's trig tutorial in the Editors Depot. That will help for creating your own scripts.  ;)
"People sleep soundly at night only because rough men stand ready to do violence on their behalf." - George Orwell

MSG Mike Everret - We Will Never Forget - '75-'08

Offline Triggerhappy

  • Contributing Member
  • **
  • Llama, it's what's for dinner.
Re:help with maths...
« Reply #7 on: 19 Oct 2005, 05:22:18 »
well i know for a fact that bedges knows trig, as he's used it before to answer other problems. perhaps this is just... mathers' block? ;D ::)

or maybe.. triggers' block
or would it be just 1 g
trigers' block

ahhh...im tired
« Last Edit: 19 Oct 2005, 05:23:18 by Triggerhappy »

Offline bedges

  • Administrator
  • *****
    • OFPEC The Editing Center
Re:help with maths...
« Reply #8 on: 19 Oct 2005, 08:02:27 »
excellent - raptorsaurus, that's just what i'm looking for.

thanks to all contributors, much appreciated. i had figured out the even-number of objects one no problem, but the odd numbers.... i just couldn't think of a simple way to include that pesky object directly in front of the player.

@triggerhappy - now who says this is for weapons, mmmm? :P and yes, the sin/cos are switched. you've also got an = instead of == ;)

thanks again to all  8)