How to slowly rotate spherical shapes using Lerp? - c#-4.0

I need to slowly rotate spherical shapes which are placed on waypoints. It needs to rotate slowly. How can I achieve this with Lerp?
The code I currently have:
if(!isWayPoint5)
{
//here i"m using turn by using rotate but i needed rotate
//slowly is same as turns train in track.
transform.Rotate(0,0,25);
isWayPoint5 = true;
}

Check out how to use Quaternion.Lerp on the wiki-site.
Using that example:
public Transform from = this.transform;
public Transform to = this.transform.Rotate(0,0,25);
public float speed = 0.1F; //You can change how fast it goes here
void Update() {
transform.rotation = Quaternion.Lerp(from.rotation, to.rotation, Time.time * speed);
}

Related

Can I let people draw with the cursor using HaxeFlixel?

basically the question. I'm trying to make a game where you need to draw certain shapes to do certain actions. I want to have the game trace the movement of the cursor, but I can't find any good way to make the lines.
I'v tried using FlxSpriteUtil.drawLine, but that doesn't seem to work. I've also tried using FlxSpriteGroups.clone() to clone a dot as the cursor, but that just leaves a trail of dots, so that doesn't work either.
I'm really new to Haxe and HaxeFlixel, so I have no idea what to do or what to use. Any suggestions?
Sounds like you started off right by using drawLine from FlxSpriteUtil. When making a drawable line, the easiest way to do it is to just save the previous mouse position in a variable, and draw a line from the previous position to the current position every frame (i.e. put the code in update()).
Here's a small code example which creates a canvas sprite which allows drawing.
package;
import flixel.FlxG;
import flixel.FlxSprite;
import flixel.FlxState;
import flixel.math.FlxPoint;
import flixel.util.FlxColor;
using flixel.util.FlxSpriteUtil;
class PlayState extends FlxState
{
private var canvas:FlxSprite;
private var lastPoint:FlxPoint;
private static inline var CANVAS_OFFSET = 16;
private static inline var DRAW_THICKNESS = 2;
private static inline var DRAW_COLOR = FlxColor.BLACK;
override public function create()
{
super.create();
canvas = new FlxSprite(CANVAS_OFFSET, CANVAS_OFFSET);
canvas.makeGraphic(256, 256, FlxColor.WHITE);
add(canvas);
}
override public function update(elapsed:Float)
{
super.update(elapsed);
if (FlxG.mouse.pressed && FlxG.mouse.overlaps(canvas))
{
if (FlxG.mouse.justPressed)
{
// This is the first point, i.e. the beginning of a new stroke
lastPoint = FlxG.mouse.getPosition();
// First, create an ellipsis to enable the player to click to draw a line
canvas.drawEllipse(
FlxG.mouse.x - CANVAS_OFFSET,
FlxG.mouse.y - CANVAS_OFFSET,
DRAW_THICKNESS / 2,
DRAW_THICKNESS / 2,
DRAW_COLOR
);
}
else
{
// Otherwise, line to the previously drawn point
var mousePos = FlxG.mouse.getPosition();
canvas.drawLine(
lastPoint.x - CANVAS_OFFSET,
lastPoint.y - CANVAS_OFFSET,
mousePos.x - CANVAS_OFFSET,
mousePos.y - CANVAS_OFFSET,
{
thickness: DRAW_THICKNESS,
color: DRAW_COLOR
}
);
lastPoint = mousePos;
}
}
}
}

is there a way i can animate a polyline?

Something similar to a pulsing ring around a marker. What I want to achieve is this...
I have many polylines all having Lat/Lng. At a time t, a user can decide to pick any polyline of their choosing. I get their current real-time location coordinates via Bluetooth. I want to use that location to determine how far they are from the polyline they have selected and depending on the distance, change/animate that polyline. I am using google maps API, android studio, kotlin
Here's a simple example - a method which takes a Polyline instance and a handler (must be on main looper) and begins a repeating update to the polyline width.
(Added the Kotlin)
private lateinit var polyline: Polyline;
private lateinit var handler : Handler;
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
var ptlist = ArrayList<LatLng>();
// add polyline
ptlist.add(LatLng(40.437761, -3.704751));
ptlist.add(LatLng( 40.405072, -3.678678));
ptlist.add(LatLng( 40.397158, -3.742706));
ptlist.add(LatLng(40.437761, -3.704751));
handler = Handler(Looper.getMainLooper());
polyline = mMap.addPolyline(PolylineOptions().addAll(ptlist));
val runnableCode = object: Runnable {
override fun run() {
var w = polyline.width;
w = w + 0.5f;
if (w > 25.0) {
w = 1.0f;
}
polyline.setWidth(w);
handler.postDelayed(this, 50);
}
}
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(LatLng(40.415521, -3.700995), 12f));
handler.postDelayed(runnableCode, 50);
}
In this example, the witdh of polyline p will vary from [25.0 1.0] incrementing by 0.5 every 200 milliseconds. When it reaches the maximum width it is reset to a width of 1.0. The original width is ignored.
Obviously change the parameters as needed - or make it pulse in and out.
(Java equivalent - animation parameters different.)
public void pulsePolyline(final Polyline p, final Handler h)
{
h.postDelayed(new Runnable() {
#Override
public void run() {
float w = p.getWidth();
w = (w + 0.1);
if (w > 10.0) w = 1.0;
p.setWidth(w);
h.postDelayed(this, 200);
}
}, 200);
}
Note the handler must be from the main thread (or looper) as in:
new Handler(Looper.getMainLooper())

Unity3D Play sound from particle

I am trying to play a sound when a particle collides with a wall. Right now, it just plays the sound from the parent object, which is the player.
However, I want the sound to play from the particle. Which means when a particle is far to the left, you vaguely hear the sound coming from the left.
Is there a way to play the sound from a particle, when it collides?
You can use OnParticleCollision and the ParticlePhysicsExtensions, and play a sound with PlayClipAtPoint:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(ParticleSystem))]
public class CollidingParticles : MonoBehaviour {
public AudioClip collisionSFX;
ParticleSystem partSystem;
ParticleCollisionEvent[] collisionEvents;
void Awake () {
partSystem = GetComponent<ParticleSystem>();
collisionEvents = new ParticleCollisionEvent[16];
}
void OnParticleCollision (GameObject other) {
int safeLength = partSystem.GetSafeCollisionEventSize();
if (collisionEvents.Length < safeLength)
collisionEvents = new ParticleCollisionEvent[safeLength];
int totalCollisions = partSystem.GetCollisionEvents(other, collisionEvents);
for (int i = 0; i < totalCollisions; i++)
AudioSource.PlayClipAtPoint(collisionSFX, collisionEvents[i].intersection);
print (totalCollisions);
}
}
The problem is that the temporary AudioSource created by PlayClipAtPoint cannot be retrieved (to set it as 3D sound). So you are better off creating your own PlayClipAtPoint method that instantiates a prefab, already configured with a 3D AudioSource and the clip you want, and run Destroy(instance, seconds) to mark it for timed destruction.
The only way I can imagine is overriding animation of particle system via GetParticles/SetParticles. Thus you can provide your own collision detection for particles with Physics.RaycastAll and play sound when collisions occured.
AudioSource audioSourcee;
public float timerToPlay;
float timerToSave;
void Start()
{
timerToSave = timerToPlay;
}
void OnEnable()
{
timerToPlay = timerToSave;
}
// Update is called once per frame
void Update()
{
if(timerToPlay>0)
timerToPlay -= Time.deltaTime;
if(timerToPlay<=0)
audioSourcee.Play();
}

HaxeFlixel: make object disappear in certain area

I'm trying to make a game where you can capture floating lights that moves randomly in the air. In the game there is going to be 3 different boxes where you can put the floating lights, so there is also going to be 3 different lights.
The lights works properly and I am able to drag them around like I want.
My issue is how to catch them and re-spawn them. I want to use the kill() method that you find in the flixel.FlxNapeSprite.
When you catch them, you should move them to the correct box, and when they come inside the box, they should get killed, you get points, and a new random light re spawn.
Link to image of the game so far
How do I kill the light-object inside a certain area?
I guess the boxes are FlxNapeSprites as well? Typically you would set up a collision callback here, which is called whenever the hitboxes of two nape bodies overlap (the light and the box in this case). You can display the nape bodies with napeDebugEnabled = true or by pressing the "N" button in the top right of flixel's debugger overlay.
Here's a simple example of how to set up a simple collision callback using Flixel + Nape:
package;
import flixel.addons.nape.FlxNapeSprite;
import flixel.addons.nape.FlxNapeState;
import flixel.util.FlxColor;
import nape.callbacks.CbEvent;
import nape.callbacks.CbType;
import nape.callbacks.InteractionCallback;
import nape.callbacks.InteractionListener;
import nape.callbacks.InteractionType;
import nape.phys.BodyType;
using flixel.util.FlxSpriteUtil;
class PlayState extends FlxNapeState
{
override public function create()
{
super.create();
bgColor = FlxColor.BLACK;
napeDebugEnabled = true;
var light = new Light(10, 10);
var box = new Box(10, 100);
add(light);
add(box);
light.body.velocity.y = 200;
FlxNapeState.space.listeners.add(new InteractionListener(
CbEvent.BEGIN,
InteractionType.COLLISION,
Light.CB_TYPE,
Box.CB_TYPE,
collideLightBox));
}
function collideLightBox(callback:InteractionCallback)
{
var light:Light = cast callback.int1.castBody.userData.sprite;
light.kill();
}
}
class Light extends FlxNapeSprite
{
public static var CB_TYPE(default, null) = new CbType();
public function new(x:Float, y:Float)
{
super(x, y);
makeGraphic(10, 10, FlxColor.TRANSPARENT);
var radius = 5;
drawCircle(5, 5, radius, FlxColor.WHITE);
createCircularBody(radius);
body.cbTypes.add(CB_TYPE);
// we need this to get the Light instance in the callback later
body.userData.sprite = this;
}
}
class Box extends FlxNapeSprite
{
public static var CB_TYPE(default, null) = new CbType();
public function new(x:Float, y:Float)
{
super(x, y);
makeGraphic(100, 50, FlxColor.GREEN);
createRectangularBody(width, height);
body.cbTypes.add(CB_TYPE);
body.type = BodyType.STATIC;
}
}
Be sure to check out the official FlxNape demo. The Nape website also has some very helpful examples + docs.

Best way to convert JTS Geometry from 3D to 2D

We are importing a Multi-Polygon Shapefile with 3D coordinates into oracle spatial using JTS Geometry Suite, GeoTools (ShapefileDataStore) and Hibernate Spatial. In Oracle Spatial we want them to be stored in 2D.
The onyl (and very slow) approach I found is the following, using WKBWriter and WKBReader:
private static Geometry convert2D(Geometry geometry3D) {
// create a 2D WKBWriter
WKBWriter writer = new WKBWriter(2);
byte[] binary = writer.write(geometry3D);
WKBReader reader = new WKBReader(factory);
Geometry geometry2D= null;
try {
geometry2D= reader.read(binary);
} catch (ParseException e) {
log.error("error reading wkb", e);
}
return geometry2D;
}
Does anybody know a more efficient way to convert a geometry from 3D to 2D?
I found a way:
Create a new CoordinateArraySequence which forces to use 2D Coordinate instances
Create a new CoordinateArraySequenceFactory which generates the new custom CoodinateArraySequence
Create a new instance of GeometryFactory which uses the new CoordinateFactory and use it to recreate the geometry:
private static Geometry convert2D(Geometry geometry3D) {
GeometryFactory geoFactory = new GeometryFactory(
geometry3d.getPrecisionModel(), geometry3d.getSRID(), CoordinateArraySequence2DFactory.instance());
if (geometry3D instanceOf Point) {
return geoFactory.createPoint(geometry3D.getCoordinateSequence());
} else if (geometry3D instanceOf Point) {
//...
//...
//...
throw new IllegalArgumentException("Unsupported geometry type: ".concat(geometry3d.getClass().getName());
}
Good luck!!
I did not test the WKBWriter and WKBReader but here is another simple approach:
Create a copy of your geometry
Set all coordinates to 2D
Simple code:
private static Geometry convert2D(Geometry g3D){
// copy geometry
Geometry g2D = (Geometry) g3D.clone();
// set new 2D coordinates
for(Coordinate c : g2D.getCoordinates()){
c.setCoordinate(new Coordinate(c.x, c.y));
}
return g2D;
}

Resources