Colobot Forum - International Colobot Community
What object is this cross in a circle? - Printable Version

+- Colobot Forum - International Colobot Community (https://colobot.info/forum)
+-- Forum: [Archive] New forum (2015-2019) (https://colobot.info/forum/forumdisplay.php?fid=76)
+--- Forum: Colobot: Gold Edition Basics (https://colobot.info/forum/forumdisplay.php?fid=58)
+---- Forum: General (https://colobot.info/forum/forumdisplay.php?fid=59)
+---- Thread: What object is this cross in a circle? (/showthread.php?tid=743)



What object is this cross in a circle? - Rinoldo - 03-18-2016

Now I'm in the 4 task of Tropica (3-th mission). I'm going to try pass it using only programming. But I couldn't find in Categories part of the Help anything about this object, cross in a circle. How can I reffer to it, and get it's direction, position.z?
[img][Image: PcVqU4D.jpg][/img]


RE: What object is this cross in a circle? - krzys_h - 03-18-2016

The category of this object is Target2, it should probably be added to the documentation somewhere


RE: What object is this cross in a circle? - Rinoldo - 03-18-2016

Thank you!
I tried to get to that cross by this code:
Code:
object tar2 = radar(Target2);
goto(tar2.position);
but there appears massage that the place is already occupied. I understand that I can use commands move, turn, but goto is much "smarter" than those. I mean it can drive round some obstacles. I also tried 
Code:
goto(tar2.position - 2);
but it says that the types are not compatible.


RE: What object is this cross in a circle? - tomaszkax86 - 03-18-2016

tar2.position is of type point, which has 3 floats. You need to save it to a variable and calculate position:

Code:
point pos = tar2.position;
pos.x += something;
pos.y += something;

You might want to use object's orientation (angle) to calculate position using cos() and sin().

Side note: goto() is kinda like cheating. It's too easy. Try doing the same using motor() and jet(). I think this could be a good idea for a programming challenge. A speed run or something.


RE: What object is this cross in a circle? - krzys_h - 03-18-2016

goto() can move only to locations on the ground (it ignores the height in point given as parameter), so you have to write your own movement algorithm using functions like jet(), turn() and move() [or motor() if you are a bit more expirenced and want to move and turn at the same time]


RE: What object is this cross in a circle? - Rinoldo - 03-18-2016

(03-18-2016, 12:50 PM)tomaszkax86 Wrote: tar2.position is of type point, which has 3 floats. You need to save it to a variable and calculate position:
You might want to use object's orientation (angle) to calculate position using cos() and sin().

I'v got some positive results, thanks! The idea to calculte position of an object is quite interesting. Could you explain with some example, please, how to get angle of an object using cos and sin? For example, Target2?

(03-18-2016, 12:50 PM)tomaszkax86 Wrote: Side note: goto() is kinda like cheating. It's too easy. Try doing the same using motor() and jet(). I think this could be a good idea for a programming challenge. A speed run or something.

Yes, I'm inclined to agree with you. But I confronted with some troubles using move(). In previous mission my WheeledShooter every time, when was stopped by a building or ruin or a bot, continued to do move() without end. Though there was errmode() in the code. In the same time, errmode() worked correctly when WheeledShooter confronted with some plant. Why in one case the game didn't take stopping as an error and in other case took it as an error and performed errmode() that is a big riddle for me. Here is that code:
Code:
object ant, power;
point pos, pos2;
float lev = 0;
ant=radar(AlienAnt);
float dir = direction(ant.position);
while(true)
{



while(radar(AlienAnt, 0, 360, 0, 20) == null)
{
ant = radar(AlienAnt);
dir = direction(ant.position);
turn(dir);
errmode(0);
int err;
err = move(distance(position, ant.position)-40);
if ( err != 0 )
{
turn(90);
move(3);
}
if (distance(position, ant.position) < 50)
{
ant = radar(AlienAnt);
dir = direction(ant.position);
turn(dir);
aim(ant.position.z);
fire(1);


}
if (energyCell.energyLevel < 0.5)
break;
}
power = radar(PowerStation);
goto(power.position);
while(energyCell.energyLevel < 1)
wait(1);
}
}



RE: What object is this cross in a circle? - tomaszkax86 - 03-18-2016

After you detected object using radar(), you can access its orientation easily:

Code:
object item = radar(Target2);
float angle = item.orientation;

Angle in degrees can be used in sin() and cos() to compute Cartesian direction. In standard math:

Code:
float radius = 5.0f;
float dx = radius * cos(angle);
float dy = radius * sin(angle);

dx and dy will be the change you need to apply to position. radius is the distance from the object. You can add dx and dy directly to base position and obtain where you need to move:

Code:
point pos = tar2.position;
pos.x += dx;
pos.y += dy;

I'm not sure if it will work like this in CBot because coordinates or orientation might be different, so you might have to test this and adapt if necessary.


RE: What object is this cross in a circle? - Rinoldo - 03-19-2016

(03-18-2016, 07:04 PM)tomaszkax86 Wrote: Angle in degrees can be used in sin() and cos() to compute Cartesian direction. In standard math:

Code:
float radius = 5.0f;
float dx = radius * cos(angle);
float dy = radius * sin(angle);

dx and dy will be the change you need to apply to position. radius is the distance from the object. 

It's not clear for me how do we know that we need to put into radius exactly 5.0f? Isn't the distance from the object changes as the bot moves? And what does means the letter f after 5.0?


RE: What object is this cross in a circle? - melex750 - 03-19-2016

The "f" is used to express a literal float value in C++, it doesn't exist in CBOT.
Code:
// orientation is the direction the object is facing. 0-360
// 0 and 360 == east
// 90 == north and 180 == west

// To calculate a point 5 meters behind an object

float dist = -5.0; // <------change this to 5.0 to go in front of the object
point pos = item.position;
float ori = item.orientation;

// calculate the difference
float dx = dist * cos(ori);  // cos() is used for x
float dy = dist * sin(ori);  // sin() is used for y

// add that to object position
pos.x += dx;
pos.y += dy;

goto(pos);
turn( direction(item.position) );



RE: What object is this cross in a circle? - Rinoldo - 03-19-2016

malex750, thanks a lot for the code! Would you be so kind to explain, why is 0 and 360 == east, 90 == north and 180 == west? As we know, in a compass 0 is north, 180 is south, 90 is east and 270 is west. How do I have to understand cardinals in the game?


RE: What object is this cross in a circle? - Simbax - 03-19-2016

https://en.wikipedia.org/wiki/Polar_coordinate_system


RE: What object is this cross in a circle? - Rinoldo - 03-26-2016

Hi everyone! I added "ori += 90" into the code above and tried:
Code:
float dist = -5.0; // <------change this to 5.0 to go in front of the object
point pos = item.position;
float ori = item.orientation;
ori += 90;
It works, but there are some problems. First of all not all crosses oriented in the same manner. So if you apply that code, the FlyingShooter will go sometimes to the front of a circle, sometimes to the rear side of the next circle. I can't figure out how to fix it. How to make it to go only to the front of a circle. By "front" I mean that side of a circle, which lies in the assumed route of FlyingShooter.


RE: What object is this cross in a circle? - krzys_h - 03-26-2016

(03-26-2016, 06:05 PM)Rinoldo Wrote: First of all not all crosses oriented in the same manner. So if you apply that code, the FlyingShooter will go sometimes to the front of a circle, sometimes to the rear side of the next circle.
This is an interesting note. It's probably related to how the objects are placed in the level file. A most simple fix would be to calculate positions on both front and back of the circle and chose the one that is closer to the robot.

If you want to see a program I wrote a long time ago to pass this mission, it's still on our old and outdated program database. If I remember correctly it's not perfect and sometimes had problems with one of the checkpoints. The comments in the program are written in Polish unfortunately. http://colobot.info/programdb/?page=view-plain&id=6