Page 1 of 1

Object Angle *SOLVED*

Posted: Wed Oct 10, 2007 10:33 am
by shadow382
I'm making an overhead shooting game and I want my character to point in the direction of the aimer. I also want the aimer to follow the cursor wherever it goes. Here's the code I have:

Code: Select all

FrameLimit 40
soldier=LoadObject("media\soldier.bmp", 72)
cross=LoadObject("media\crosshair.bmp", 72)

Repeat
  PositionObject cross,Cos(MouseX()),Sin(MouseY())
  PointObject soldier,cross
  If UpKey() Then MoveObject soldier,2
  If DownKey() Then MoveObject soldier,-2
  DrawScreen
Until EscapeKey()
If you load that I'm sure you'll see my problem. I'm just starting out with CoolBasic and I know I must be doing something wrong. So any help is appreciated!

Re: Object Angle

Posted: Wed Oct 10, 2007 10:56 am
by Astigma
You cannot take cos or sin from coordinates or at least it does not make any sense. The parameter of sin and cos is angle, so you must put an angle to them. The only angle I can imagine is the angle from soldier to mouse's world coorindates. Here is code which seems to work correctly:

Code: Select all

FrameLimit 40
soldier=LoadObject("media\soldier.bmp", 72)
cross=LoadObject("media\crosshair.bmp", 72)

Repeat
  angle# = GetAngle(ObjectX(soldier), ObjectY(soldier), MouseWX(), MouseWY())
  PositionObject cross,ObjectX(soldier)+Cos(angle)*40,ObjectY(soldier)-Sin(angle)*40
  PointObject soldier,cross
  If UpKey() Then MoveObject soldier,2
  If DownKey() Then MoveObject soldier,-2
  DrawScreen
Until EscapeKey()

Re: Object Angle

Posted: Wed Oct 10, 2007 11:09 am
by shadow382
Thank you, that works perfectly!