Object Angle *SOLVED*

Post your coding questions such as 'how to' here.
Post Reply
shadow382
Newcomer
Posts: 2
Joined: Sun Oct 07, 2007 10:28 am
Location: Temple, GA
Contact:

Object Angle *SOLVED*

Post 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!
Last edited by shadow382 on Wed Oct 10, 2007 11:09 am, edited 1 time in total.
Astigma
Moderator
Moderator
Posts: 195
Joined: Sun Aug 26, 2007 5:56 pm
Location: Kuopio, Finland
Contact:

Re: Object Angle

Post 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()
shadow382
Newcomer
Posts: 2
Joined: Sun Oct 07, 2007 10:28 am
Location: Temple, GA
Contact:

Re: Object Angle

Post by shadow382 »

Thank you, that works perfectly!
Post Reply